Skip to content

Commit

Permalink
use a new arg parsing library
Browse files Browse the repository at this point in the history
  • Loading branch information
filipw authored and khellang committed Aug 15, 2018
1 parent e533a9d commit 9e49b72
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/ScriptCs.Core/FrameworkUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static string FrameworkName
if (_frameworkName == null)
{
//Thanks to Dave Glick for this code contribution
var frameworkName = Assembly.GetExecutingAssembly().GetCustomAttributes(true)
var frameworkName = Assembly.GetEntryAssembly().GetCustomAttributes(true)
.OfType<System.Runtime.Versioning.TargetFrameworkAttribute>()
.Select(x => x.FrameworkName)
.FirstOrDefault();
Expand Down
2 changes: 1 addition & 1 deletion src/ScriptCs.Core/ReplCommands/InstallCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public object Execute(IRepl repl, object[] args)
_installationProvider.Initialize();

var packageRef = new PackageReference(
args[0].ToString(), new FrameworkName(".NETFramework,Version=v4.0"), version);
args[0].ToString(), new FrameworkName(".NETFramework,Version=v4.6.1"), version);

_packageInstaller.InstallPackages(new[] { packageRef }, allowPre);
_packageAssemblyResolver.SavePackages();
Expand Down
2 changes: 1 addition & 1 deletion src/ScriptCs.Hosting/Package/PackageContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private static bool IsValidFramework(FrameworkName frameworkName)

private static bool IsValidProfile(string profile)
{
return profile == "net40" || profile == "net45";
return profile == "net40" || profile == "net45" || profile == "net461" || profile == "netstandard20";
}
}
}
10 changes: 10 additions & 0 deletions src/ScriptCs/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public static Config Create(ScriptCsArgs commandArgs)
.Apply(ConfigMask.Create(commandArgs));
}

public static Config Create(string maskFile, ConfigMask configMask)
{
Guard.AgainstNullArgument("configMask", configMask);

return new Config()
.Apply(ConfigMask.ReadGlobalOrDefault())
.Apply(maskFile == null ? ConfigMask.ReadLocalOrDefault() : ConfigMask.Read(maskFile))
.Apply(configMask);
}

public Config Apply(ConfigMask mask)
{
if (mask == null)
Expand Down
104 changes: 83 additions & 21 deletions src/ScriptCs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using McMaster.Extensions.CommandLineUtils;
using ScriptCs.Contracts;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand All @@ -10,40 +12,100 @@ internal static class Program
[LoaderOptimizationAttribute(LoaderOptimization.MultiDomain)]
private static int Main(string[] args)
{
//args = args.Skip(2).ToArray();
ProfileOptimizationShim.SetProfileRoot(Path.GetDirectoryName(typeof(Program).Assembly.Location));
ProfileOptimizationShim.StartProfile(typeof(Program).Assembly.GetName().Name + ".profile");

var nonScriptArgs = args.TakeWhile(arg => arg != "--").ToArray();
var scriptArgs = args.Skip(nonScriptArgs.Length + 1).ToArray();
ScriptCsArgs commandArgs;
try

var app = new CommandLineApplication(throwOnUnexpectedArg: true)
{
commandArgs = ScriptCsArgs.Parse(nonScriptArgs);
}
catch (Exception ex)
ExtendedHelpText = "Usage: scriptcs options",
OptionsComparison = StringComparison.OrdinalIgnoreCase
};

var script = app.Argument("script", "Script file name, must be specified first");

var scriptNameFallback = app.Option("--scriptname | -script", "Alternative way to pass a script filename", CommandOptionType.NoValue);
var repl = app.Option("--repl | -r", "Launch REPL mode when running script. To just launch REPL, simply omit the 'script' argument", CommandOptionType.NoValue);
var eval = app.Option("--eval | -e", "Code to immediately evaluate", CommandOptionType.SingleValue);
var configFile = app.Option("--config | -co", "Defines config file name", CommandOptionType.SingleValue);
var debug = app.Option("--debug | -d", "Emits PDB symbols allowing for attaching a Visual Studio debugger", CommandOptionType.NoValue);
var version = app.Option("--version | -v", "Outputs version information", CommandOptionType.NoValue);
var cache = app.Option("--cache | -c", "Flag which determines whether to run in memory or from a .dll", CommandOptionType.NoValue);
var logLevel = app.Option<LogLevel?>("--loglevel | -log", "Flag which defines the log level used", CommandOptionType.SingleValue);
var watch = app.Option("--watch | -w", "Watch the script file and reload it when changed", CommandOptionType.NoValue);
var modules = app.Option("--modules | -m", "Specify modules to load (comma separated)", CommandOptionType.SingleValue);
var output = app.Option("--output | -o", "Write all console output to the specified file", CommandOptionType.SingleValue);

app.HelpOption("-Help | -?");

app.Command("install", c =>
{
Console.WriteLine(ex.Message);
return 1;
}
var package = c.Argument("package", "Specific package to install, otherwise installs and restores packages which are specified in packages.config");
var allowPrerelease = c.Option("--allowprerelease | -pre", "Allows installation of packages' prelease versions", CommandOptionType.NoValue);
var packageVersion = c.Option("--packageversion | -p", "Defines the version of the package to install", CommandOptionType.SingleValue);
var save = c.Option("--save | -s", "Creates a packages.config file based on the packages directory", CommandOptionType.NoValue);
var clean = c.Option("--clean | -cl", "Cleans installed packages from working directory", CommandOptionType.NoValue);
var global = c.Option("--global | -g", "Installs and restores global packages which are specified in packages.config", CommandOptionType.NoValue);
if (commandArgs.Help)
c.OnExecute(() =>
{
var configMask = new ConfigMask();
configMask.Repl = false;
configMask.Global = global.HasValue() ? true : (bool?)null;
configMask.Install = package.Value ?? string.Empty; // needed to trigger install of all packages!
configMask.PackageVersion = packageVersion.Value();
configMask.AllowPreRelease = allowPrerelease.HasValue() ? true : (bool?)null;
configMask.Save = save.HasValue() ? true : (bool?)null;
configMask.Global = global.HasValue() ? true : (bool?)null;
configMask.Clean = clean.HasValue() ? true : (bool?)null;
configMask.Output = output.Value();
configMask.Debug = debug.HasValue() ? true : (bool?)null;
configMask.LogLevel = logLevel.ParsedValue;
return Application.Run(Config.Create(configFile.Value(), configMask), scriptArgs);
});
});

app.OnExecute(() =>
{
Console.WriteLine(ScriptCsArgs.GetUsage());
return 0;
}
if (configFile.HasValue() && !File.Exists(configFile.Value()))
{
Console.WriteLine("The specified config file does not exist.");
return 1;
}
if (version.HasValue())
{
VersionWriter.Write();
return 0;
}
var configMask = new ConfigMask();
configMask.Repl = repl.HasValue() ? true : (bool?)null;
configMask.ScriptName = scriptNameFallback.HasValue() ? scriptNameFallback.Value() : script.Value;
configMask.Debug = debug.HasValue() ? true : (bool?)null;
configMask.Eval = eval.Value();
configMask.Cache = cache.HasValue() ? true : (bool?)null;
configMask.Watch = watch.HasValue() ? true : (bool?)null;
configMask.LogLevel = logLevel.ParsedValue;
configMask.Modules = modules.Value();
configMask.Output = output.Value();
if (commandArgs.Version)
return Application.Run(Config.Create(configFile.Value(), configMask), scriptArgs);
});

try
{
VersionWriter.Write();
return 0;
return app.Execute(nonScriptArgs);
}

if (commandArgs.Config != null && !File.Exists(commandArgs.Config))
catch (CommandParsingException e)
{
Console.WriteLine("The specified config file does not exist.");
app.ShowHelp();
return 1;
}

return Application.Run(Config.Create(commandArgs), scriptArgs);
}
}
}
6 changes: 6 additions & 0 deletions src/ScriptCs/ScriptCs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<Compile Include="..\ScriptCs.Core\Guard.cs" Link="Guard.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.2.5" />
<PackageReference Include="PowerArgs" Version="3.0.0" />
</ItemGroup>
<ItemGroup>
Expand All @@ -26,4 +27,9 @@
<ProjectReference Include="..\ScriptCs.Engine.Roslyn\ScriptCs.Engine.Roslyn.csproj" />
<ProjectReference Include="..\ScriptCs.Hosting\ScriptCs.Hosting.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
</None>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion test/ScriptCs.Tests.Acceptance/CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void UnexpectedOption(Exception exception)
.x(() =>
{
Console.WriteLine(exception);
exception.Message.ShouldContain("unknownoption");
exception.Message.ShouldContain("Usage: scriptcs options");
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/ScriptCs.Tests.Acceptance/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void CustomConfiguration(ScenarioDirectory directory, string outpu
"When I execute the script without the log level option but specifying the custom config"
.x(() =>
{
var args = new[] { "-config", "custom.opts", };
var args = new[] { "--config", "custom.opts", };
output = ScriptCsExe.Run("foo.csx", false, args, directory);
});

Expand Down
8 changes: 4 additions & 4 deletions test/ScriptCs.Tests.Acceptance/Support/ScriptCsExe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static string Run(string scriptName, bool debug, IEnumerable<string> args
debug &&
!args.Select(arg => arg.Trim().ToUpperInvariant()).Contains("-DEBUG") &&
!args.Select(arg => arg.Trim().ToUpperInvariant()).Contains("-D")
? new[] { "-debug" }
? new[] { "--debug" }
: new string[0];

return Execute(
Expand Down Expand Up @@ -102,17 +102,17 @@ public static string Install(string package, ScenarioDirectory directory)
writer.Flush();
}

return Execute(new[] { "-install", package }, Enumerable.Empty<string>(), directory);
return Execute(new[] { "install", package }, Enumerable.Empty<string>(), directory);
}

public static string Save(ScenarioDirectory directory)
{
return Execute(new[] { "-save" }, Enumerable.Empty<string>(), directory);
return Execute(new[] { "install", "--save" }, Enumerable.Empty<string>(), directory);
}

public static string Clean(ScenarioDirectory directory)
{
return Execute(new[] { "-clean" }, Enumerable.Empty<string>(), directory);
return Execute(new[] { "install", "--clean" }, Enumerable.Empty<string>(), directory);
}

private static string Execute(
Expand Down

0 comments on commit 9e49b72

Please sign in to comment.