Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/ScriptCs.Core/FrameworkUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ public static string FrameworkName
{
if (_frameworkName == null)
{
// in order to handle the weird behavior of old nuget packages
// with NET Standard 2.0, we'll use the entry assembly if possible
var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();

//Thanks to Dave Glick for this code contribution
var frameworkName = Assembly.GetExecutingAssembly().GetCustomAttributes(true)
var frameworkName = assembly.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";
}
}
}
8 changes: 4 additions & 4 deletions src/ScriptCs/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public string[] Modules

public string Eval { get; set; }

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

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

public Config Apply(ConfigMask mask)
Expand Down
24 changes: 0 additions & 24 deletions src/ScriptCs/ConfigMask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,6 @@ public class ConfigMask

public bool? Watch { get; set; }

public static ConfigMask Create(ScriptCsArgs args)
{
Guard.AgainstNullArgument("args", args);

return new ConfigMask
{
AllowPreRelease = args.AllowPreRelease ? (bool?)true : null,
Cache = args.Cache ? (bool?)true : null,
Clean = args.Clean ? (bool?)true : null,
Debug = args.Debug ? (bool?)true : null,
Global = args.Global ? (bool?)true : null,
Install = args.Install,
LogLevel = args.LogLevel,
Modules = args.Modules,
Output = args.Output,
PackageVersion = args.PackageVersion,
Repl = args.Repl ? (bool?)true : null,
Save = args.Save ? (bool?)true : null,
ScriptName = args.ScriptName,
Eval = args.Eval,
Watch = args.Watch ? (bool?)true : null,
};
}

public static ConfigMask ReadGlobalOrDefault() => Read(new FileSystem().GlobalOptsFile, true);

public static ConfigMask ReadLocalOrDefault() => Read(Constants.ConfigFilename, true);
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);
}
}
}
2 changes: 1 addition & 1 deletion src/ScriptCs/ScriptCs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Compile Include="..\ScriptCs.Core\Guard.cs" Link="Guard.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="PowerArgs" Version="3.0.0" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.2.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ScriptCs.Contracts\ScriptCs.Contracts.csproj" />
Expand Down
111 changes: 0 additions & 111 deletions src/ScriptCs/ScriptCsArgs.cs

This file was deleted.

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(
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
Loading