diff options
author | burdoto | 2023-05-08 01:49:02 +0200 |
---|---|---|
committer | burdoto | 2023-05-08 01:49:02 +0200 |
commit | d31a50b6211b2b3b29d8afcb067d04035023a66c (patch) | |
tree | 7e37c3e7030a48f7766cad881de5e24e61d5d8e3 | |
parent | ca450f1f5f6d3811bea6af3624eab76947bfdae9 (diff) | |
download | aur-d31a50b6211b2b3b29d8afcb067d04035023a66c.tar.gz |
update
-rw-r--r-- | .gitattributes | 2 | ||||
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Commands.cs | 43 | ||||
-rw-r--r-- | PKGBUILD | 8 | ||||
-rw-r--r-- | RGX.cs | 121 | ||||
-rw-r--r-- | clean.sh | 7 | ||||
-rw-r--r-- | dev.sh | 7 | ||||
-rwxr-xr-x | publish.sh | 15 | ||||
-rw-r--r-- | rgx.csproj | 10 |
9 files changed, 125 insertions, 89 deletions
diff --git a/.gitattributes b/.gitattributes index d68b03ad6cb9..dfe0770424b2 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,2 @@ # Auto detect text files and perform LF normalization * text=auto -*.sh text -PKGBUILD text diff --git a/.gitignore b/.gitignore index c7bdbb346e1e..81ac31362c00 100644 --- a/.gitignore +++ b/.gitignore @@ -683,4 +683,5 @@ fabric.properties clmath/Antlr/ winbash-util/winbash-util.csproj.DotSettings +.build/ *.tar.gz diff --git a/Commands.cs b/Commands.cs new file mode 100644 index 000000000000..e82a890fa106 --- /dev/null +++ b/Commands.cs @@ -0,0 +1,43 @@ +using System.Text.RegularExpressions; +using CommandLine; + +namespace rgx; + +internal interface ICmd +{ + [Value(0, MetaName = "pattern", Required = true)] + public string pattern { get; set; } + + [Option('x', "options", Separator = ',', Required = false)] + public IEnumerable<RegexOptions> options { get; set; } + + [Option('i', "input", Required = false, Default = null)] + public string? input { get; set; } + + [Option('o', "output", Required = false, Default = null)] + public string? output { get; set; } +} + +[Verb("-M", true, new[] { "-R" })] +internal class MatchAndReplace : ICmd +{ + [Option('d', "default", Required = false, Default = null)] + public bool useDefault { get; set; } + + [Value(1, MetaName = "replacement", Required = false, Default = null)] + public string? replacement { get; set; } + + public string pattern { get; set; } + public IEnumerable<RegexOptions> options { get; set; } + public string? input { get; set; } + public string? output { get; set; } +} + +[Verb("-S")] +internal class Split : ICmd +{ + public string pattern { get; set; } + public IEnumerable<RegexOptions> options { get; set; } + public string? input { get; set; } + public string? output { get; set; } +}
\ No newline at end of file @@ -1,8 +1,8 @@ # Maintainer: Tobias Burdow <kaleidox@comroid.org> pkgname=rgx-git -pkgver=0.1 -pkgrel=3 +pkgver=0.2 +pkgrel=1 pkgdesc="CLI RegExp Tool" arch=('any') url="https://github.com/comroid-git/rgx" @@ -14,10 +14,10 @@ md5sums=('SKIP') options+=("!strip") build() { - cd "$srcdir/rgx" + cd rgx dotnet publish -c Release --use-current-runtime } package() { - install -Dm755 "$srcdir/rgx/bin/Release/net6.0/linux-x64/publish/rgx" "$pkgdir/usr/bin/rgx" + install -Dm755 "rgx/bin/Release/net6.0/linux-x64/publish/rgx" "$pkgdir/usr/bin/rgx" } @@ -1,23 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; +using System.Globalization; using System.Text.RegularExpressions; using CommandLine; -using comroid.common; namespace rgx; public static class RGX { - private static readonly ILog log = new Log("rgx"); - - static RGX() - { - ILog.Detail = DetailLevel.None; - } - public static void Main(string[] args) { new Parser(cfg => @@ -31,72 +19,75 @@ public static class RGX cfg.ParsingCulture = CultureInfo.InvariantCulture; cfg.EnableDashDash = false; cfg.MaximumDisplayWidth = Console.WindowWidth; - }).ParseArguments<Arg>(args) - .WithParsed(Run) + }).ParseArguments<MatchAndReplace, Split, Group>(args) + .WithParsed<MatchAndReplace>(RunMatch) + .WithParsed<Split>(RunSplit) .WithNotParsed(Error); } - private static void Run(Arg cmd) + private static (Regex pattern, TextReader input, TextWriter output) Prepare(ICmd cmd) { - log.At(LogLevel.Info, "excess: " + cmd.excess); + return (new Regex(cmd.pattern, (RegexOptions)cmd.options.Aggregate(0, (x, y) => x | (int)y)), + cmd.input is not null and not "" + ? File.Exists(cmd.input) + ? new StreamReader(new FileStream(cmd.input, FileMode.Open, FileAccess.Read)) + : new StringReader(cmd.input) + : Console.In, + cmd.output is not null and not "" && File.Exists(cmd.output) + ? new StreamWriter(new FileStream(cmd.output, FileMode.Truncate, FileAccess.Write)) + : Console.Out); + } - var pattern = new Regex(cmd.pattern, (RegexOptions)cmd.options.Aggregate(0, (x, y) => x | (int)y)); - using var input = cmd is { split: true, replacement: not null } ? new StringReader(cmd.replacement) : Console.In; - using var output = cmd.fileOutput is not null ? new StreamWriter(new FileStream(cmd.fileOutput, FileMode.Truncate, FileAccess.Write)) : Console.Out; + private static void RunMatch(MatchAndReplace cmd) + { + var (pattern, input, output) = Prepare(cmd); + + while (input.ReadLine() is { } line) + if (pattern.IsMatch(line)) + output.WriteLine(cmd.replacement == null ? line : pattern.Replace(line, cmd.replacement)); + else if (cmd.useDefault) + output.WriteLine(line); + + foreach (var res in new IDisposable[] { input, output }) + res.Dispose(); + } - if (cmd.split) + private static void RunSplit(Split cmd) + { + // ReSharper disable once RedundantAssignment + var (pattern, input, output) = Prepare(cmd); + + var newPattern = cmd.pattern; + if (newPattern.StartsWith('^')) + // do not match beginning of string + newPattern = newPattern.Substring(1); + if (!newPattern.EndsWith('$')) + newPattern += '$'; + pattern = new Regex(newPattern); + int c; + var buf = new StringWriter(); + while ((c = input.Read()) != -1) { - var newPattern = cmd.pattern; - if (newPattern.StartsWith('^')) - // do not match beginning of string - newPattern = newPattern.Substring(1); - if (!newPattern.EndsWith('$')) - newPattern += '$'; - pattern = new Regex(newPattern); - int c; - var buf = new StringWriter(); - while ((c = input.Read()) != -1) + buf.Write((char)c); + var str = buf.ToString(); + if (pattern.Match(str) is not { Success: true } match) { - buf.Write((char)c); - var str = buf.ToString(); - if (pattern.Match(str) is not { Success: true } match) - { - output.Write((char)c); - continue; - } - if (!string.IsNullOrWhiteSpace(str.Remove(match.Index, match.Length))) - output.WriteLine(); - buf.Close(); - buf = new StringWriter(); + output.Write((char)c); + continue; } + if (!string.IsNullOrWhiteSpace(str.Remove(match.Index, match.Length))) + output.WriteLine(); + buf.Close(); + buf = new StringWriter(); } - else - while (input.ReadLine() is { } line) - if (pattern.IsMatch(line)) - output.WriteLine(cmd.replacement == null ? line : pattern.Replace(line, cmd.replacement)); + + foreach (var res in new IDisposable[] { input, output }) + res.Dispose(); } private static void Error(IEnumerable<Error> errors) { foreach (var err in errors) - log.At(err.StopsProcessing ? LogLevel.Fatal : LogLevel.Error, err); - } - - private class Arg - { - [Value(0, MetaName = "pattern", Required = true)] - public string pattern { get; set; } = null!; - - [Value(1, MetaName = "replacement", Required = false, Default = null)] - public string? replacement { get; set; } - [Value(1)] - public IEnumerable<string> excess { get; set; } - - [Option(shortName: 'o', longName: "options", Separator = ',', Required = false)] - public IEnumerable<RegexOptions> options { get; set; } = null!; - [Option(shortName: 's', longName: "split", Required = false, Default = false)] - public bool split { get; set; } - [Option(shortName: 'f', longName: "file", Required = false, Default = null)] - public string? fileOutput { get; set; } + Console.Error.WriteLine(err); } }
\ No newline at end of file diff --git a/clean.sh b/clean.sh new file mode 100644 index 000000000000..4540329125c8 --- /dev/null +++ b/clean.sh @@ -0,0 +1,7 @@ +#!/usr/bin/bash + +rm -rf */ || true +rm *.tar* || true +dotnet clean || true +git clean -f || true +git reset --hard || true diff --git a/dev.sh b/dev.sh new file mode 100644 index 000000000000..feeb33e6a83b --- /dev/null +++ b/dev.sh @@ -0,0 +1,7 @@ +#!/usr/bin/bash + +set -e + +./clean.sh +dotnet publish -c Release --use-current-runtime || echo "Build failed" >&2 +bin/Release/net6.0/linux-x64/publish/rgx || echo "Execute failed" >&2 diff --git a/publish.sh b/publish.sh index 86f794d9fd22..bbc0521b0d86 100755 --- a/publish.sh +++ b/publish.sh @@ -1,19 +1,18 @@ -#!/bin/bash +#!/usr/bin/bash set -e # exit on error -# make prebuilt executable -dotnet clean -dotnet publish -c Release --use-current-runtime +./clean.sh + +# run tests first +dotnet test # update SRCINFO -git clean -f -git reset --hard makepkg --printsrcinfo > .SRCINFO (git add .SRCINFO && git commit -m "SRCINFO" && git push) || true -# verify that makepkg works -makepkg -Cc --noconfirm +# build the executable +makepkg -Cf --noconfirm # push to aur if [ -z "$(git remote | grep aur)" ]; then diff --git a/rgx.csproj b/rgx.csproj index dda0477a9560..9ed4ccfd4109 100644 --- a/rgx.csproj +++ b/rgx.csproj @@ -11,17 +11,7 @@ <FileVersion>0.10</FileVersion> <PublishSingleFile>true</PublishSingleFile> <SelfContained>false</SelfContained> - - <LibCommonPath>..\csapi\common\common.csproj</LibCommonPath> </PropertyGroup> - - <ItemGroup Condition="Exists('$(LibCommon)')"> - <ProjectReference Include="$(LibCommon)" /> - </ItemGroup> - - <ItemGroup Condition="!Exists('$(LibCommon)')"> - <PackageReference Include="comroid.csapi.common" Version="0.1.0" /> - </ItemGroup> <ItemGroup> <PackageReference Include="CommandLineParser" Version="2.9.1" /> |