Skip to content

Commit

Permalink
serge clean-ts: Add --ts-dir (--ts-dirs) parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
iafan committed Nov 9, 2020
1 parent 0d58250 commit 2092f87
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ Pending release
- Added `comment_attrs` property for `parse_xml` parser to allow for
easy comment extraction from a set of attributes of a wrapper node.

- `serge clean-ts` now can accept `--ts-dir=path` (or
`--ts-dirs=path,path2,...`) parameters. The parameters allow one
to explicitly specify a list of directories to scan and clean.

1.4 July 30, 2019

- Support `--lang` parameter in `serge pull-ts` and `serge push-ts`
Expand Down
14 changes: 13 additions & 1 deletion doc/html/serge-clean-ts.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1 id="NAME">NAME</h1>

<h1 id="SYNOPSIS">SYNOPSIS</h1>

<p><code>serge clean-ts [configuration-files] [--dry-run] [--for-each=&quot;do something with &#39;[PATH]&#39;&quot;]</code></p>
<p><code>serge clean-ts [configuration-files] [--dry-run] [--for-each=&quot;do something with &#39;[PATH]&#39;&quot;] [--ts-dir=&lt;path&gt;]</code></p>

<p>Where <code>[configuration-files]</code> is a path to a specific .serge file, or a directory to scan .serge files in. You can specify multiple paths as separate command-line parameters. If no paths provided, Serge will look up for .serge files in the current directory.</p>

Expand All @@ -41,6 +41,18 @@ <h1 id="OPTIONS">OPTIONS</h1>

<p>Just report orphaned translation files, do not delete them.</p>

</dd>
<dt><b>--ts-dir=path[,path2]</b>, <b>--ts-dirs=path[,path2]</b></dt>
<dd>

<p>An optional comma-separated list of directories with translation interchange files to scan and clean up.</p>

<p>By default, Serge will determine this list automatically by gathering <code>ts_file_path</code> paths from found .serge configuration files. In certain cases, however, it may fail to do so due to custom path rewriting logic that may tell Serge to save translation interchange files outside of the common <code>ts_file_path</code> root.</p>

<p>Note that the path can be specified either as a relative to the current working directory, or an absolute one, but exactly how it resolves by Serge (use <code>--dry-run</code> option to see the list of reported files and their paths).</p>

<p>For example, if your paths resolve to <code>/var/data/serge/configs/../ts/myproject/file.po</code>, you can specify <code>/var/data/serge/configs/../ts</code> as a value for this parameter, or you can specify <code>../ts</code> while being inside <code>/var/data/serge/configs</code> directory, but <code>/var/data/serge/ts</code> won&#39;t work.</p>

</dd>
<dt><b>--for-each=command</b></dt>
<dd>
Expand Down
23 changes: 22 additions & 1 deletion doc/pod/serge-clean-ts.pod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ serge-clean-ts - Delete orphaned translation files
=head1 SYNOPSIS

C<< serge clean-ts [configuration-files]
[--dry-run] [--for-each="do something with '[PATH]'"] >>
[--dry-run] [--for-each="do something with '[PATH]'"] [--ts-dir=<path>] >>

Where C<< [configuration-files] >> is a path to a specific .serge file,
or a directory to scan .serge files in. You can specify multiple paths
Expand All @@ -27,6 +27,27 @@ then delete all unknown (orphaned) files from those target directories.

Just report orphaned translation files, do not delete them.

=item B<--ts-dir=path[,path2]>, B<--ts-dirs=path[,path2]>

An optional comma-separated list of directories with translation interchange
files to scan and clean up.

By default, Serge will determine this list automatically
by gathering C<< ts_file_path >> paths from found .serge configuration files.
In certain cases, however, it may fail to do so due to custom path rewriting
logic that may tell Serge to save translation interchange files outside of
the common C<< ts_file_path >> root.

Note that the path can be specified either as a relative to the current working
directory, or an absolute one, but exactly how it resolves by Serge
(use C<< --dry-run >> option to see the list of reported files and their paths).

For example, if your paths resolve to
C<< /var/data/serge/configs/../ts/myproject/file.po >>, you can specify
C<< /var/data/serge/configs/../ts >> as a value for this parameter, or you
can specify C<< ../ts >> while being inside C<< /var/data/serge/configs >>
directory, but C<< /var/data/serge/ts >> won't work.

=item B<--for-each=command>

For each deleted file or deleted empty folder, run the specified command.
Expand Down
25 changes: 22 additions & 3 deletions lib/Serge/Command/clean_ts.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sub init {
$self->SUPER::init($command);

GetOptions(
"ts-dir|ts-dirs=s" => \$self->{ts_directories},
"dry-run" => \$self->{dry_run},
"for-each:s" => \$self->{for_each},
) or die "Failed to parse some command-line parameters.";
Expand Down Expand Up @@ -55,9 +56,27 @@ sub run {
$processor->run();
}

print "\nFound translation directories:\n";
foreach (sort keys %{$scanner->{ts_directories}}) {
if ($self->{ts_directories}) {
my @dirs = split(/,/, $self->{ts_directories});
map {
my $path = $_;
$path = abspath(normalize_path($path));
$ts_directories{$path} = 1;
} @dirs;
} else {
%ts_directories = %{$scanner->{ts_directories}};
}

if ($self->{ts_directories}) {
print "\nUsing explicitly specified translation directories:\n";
} else {
print "\nFound translation directories:\n";
}
foreach (sort keys %ts_directories) {
print "\t$_\n";
if (!-d) {
print "\t\tWARNING: Directory $_ doesn't exist\n";
}
}

print "\nScanning translation files...";
Expand All @@ -67,7 +86,7 @@ sub run {
push @ts_files, $File::Find::name if (-f $_ && /\.po$/); # TODO: refactor; file extensions should not be hard-coded
};

foreach my $dir (sort keys %{$scanner->{ts_directories}}) {
foreach my $dir (sort keys %ts_directories) {
finddepth({wanted => $wanted, follow => 1}, $dir);
}
$n = scalar(@ts_files);
Expand Down

0 comments on commit 2092f87

Please sign in to comment.