Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Commit

Permalink
Accept arrays as values of scripts in project.json
Browse files Browse the repository at this point in the history
  • Loading branch information
ChengTian committed Oct 2, 2014
1 parent 4fff016 commit f96e202
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
59 changes: 31 additions & 28 deletions src/Microsoft.Framework.PackageManager/Scripts/ScriptExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,44 @@ public class ScriptExecutor
{
public void Execute(Runtime.Project project, string scriptName, Func<string, string> getVariable)
{
string scriptCommandLine;
if (!project.Scripts.TryGetValue(scriptName, out scriptCommandLine))
IEnumerable<string> scriptCommandLines;
if (!project.Scripts.TryGetValue(scriptName, out scriptCommandLines))
{
return;
}

var scriptArguments = CommandGrammar.Process(
scriptCommandLine,
GetScriptVariable(project, getVariable));

// Command-lines on Windows are executed via "cmd /C" in order
// to support batch files, &&, built-in commands like echo, etc.
// ComSpec is Windows-specific, and contains the full path to cmd.exe
var comSpec = Environment.GetEnvironmentVariable("ComSpec");
if (!string.IsNullOrEmpty(comSpec))
foreach (var scriptCommandLine in scriptCommandLines)
{
scriptArguments =
new[] { comSpec, "/C", "\"" }
.Concat(scriptArguments)
.Concat(new[] { "\"" })
.ToArray();
}
var scriptArguments = CommandGrammar.Process(
scriptCommandLine,
GetScriptVariable(project, getVariable));

var startInfo = new ProcessStartInfo
{
FileName = scriptArguments.FirstOrDefault(),
Arguments = String.Join(" ", scriptArguments.Skip(1)),
WorkingDirectory = project.ProjectDirectory,
#if NET45
UseShellExecute = false,
#endif
};
var process = Process.Start(startInfo);
// Command-lines on Windows are executed via "cmd /C" in order
// to support batch files, &&, built-in commands like echo, etc.
// ComSpec is Windows-specific, and contains the full path to cmd.exe
var comSpec = Environment.GetEnvironmentVariable("ComSpec");
if (!string.IsNullOrEmpty(comSpec))
{
scriptArguments =
new[] { comSpec, "/C", "\"" }
.Concat(scriptArguments)
.Concat(new[] { "\"" })
.ToArray();
}

var startInfo = new ProcessStartInfo
{
FileName = scriptArguments.FirstOrDefault(),
Arguments = String.Join(" ", scriptArguments.Skip(1)),
WorkingDirectory = project.ProjectDirectory,
#if NET45
UseShellExecute = false,
#endif
};
var process = Process.Start(startInfo);

process.WaitForExit();
process.WaitForExit();
}
}

private Func<string, string> GetScriptVariable(Runtime.Project project, Func<string, string> getVariable)
Expand Down
19 changes: 16 additions & 3 deletions src/Microsoft.Framework.Runtime/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class Project
public Project()
{
Commands = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Scripts = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Scripts = new Dictionary<string, IEnumerable<string>>(StringComparer.OrdinalIgnoreCase);
}

public string ProjectFilePath { get; private set; }
Expand Down Expand Up @@ -197,7 +197,7 @@ public IEnumerable<string> ContentFiles

public IDictionary<string, string> Commands { get; private set; }

public IDictionary<string, string> Scripts { get; private set; }
public IDictionary<string, IEnumerable<string>> Scripts { get; private set; }

public IEnumerable<TargetFrameworkInformation> GetTargetFrameworks()
{
Expand Down Expand Up @@ -308,7 +308,20 @@ public static Project GetProject(string json, string projectName, string project
{
foreach (var script in scripts)
{
project.Scripts[script.Key] = script.Value.ToObject<string>();
var value = script.Value;
if (value.Type == JTokenType.String)
{
project.Scripts[script.Key] = new string[] { value.ToObject<string>() };
}
else if (value.Type == JTokenType.Array)
{
project.Scripts[script.Key] = script.Value.ToObject<string[]>();
}
else
{
throw new InvalidDataException(string.Format(
"The value of a script in {0} can only be a string or an array of strings", ProjectFileName));
}
}
}

Expand Down

0 comments on commit f96e202

Please sign in to comment.