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

Add '--wwwroot' and '--wwwroot-out' option to "kpm pack" #622

Merged
merged 1 commit into from
Sep 12, 2014
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
28 changes: 27 additions & 1 deletion src/Microsoft.Framework.PackageManager/Packing/PackManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,31 @@ public bool Package()
return false;
}

// '--wwwroot' option can override 'webroot' property in project.json
_options.WwwRoot = _options.WwwRoot ?? project.WebRoot;
_options.WwwRootOut = _options.WwwRootOut ?? _options.WwwRoot;

if (string.IsNullOrEmpty(_options.WwwRoot) && !string.IsNullOrEmpty(_options.WwwRootOut))
{
Console.WriteLine("'--wwwroot-out' option can be used only when the '--wwwroot' option or 'webroot' in project.json is specified.");
return false;
}

if (!string.IsNullOrEmpty(_options.WwwRoot) &&
!Directory.Exists(Path.Combine(project.ProjectDirectory, _options.WwwRoot)))
{
Console.WriteLine("The specified wwwroot folder '{0}' doesn't exist in the project directory.",
_options.WwwRoot);
return false;
}

if (string.Equals(_options.WwwRootOut, PackRoot.AppRootName, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("'{0}' is a reserved folder name. Please choose another name for the wwwroot-out folder.",
PackRoot.AppRootName);
return false;
}

var sw = Stopwatch.StartNew();

string outputPath = _options.OutputDir ?? Path.Combine(_options.ProjectDir, "bin", "output");
Expand Down Expand Up @@ -206,7 +231,8 @@ public bool Package()
var packProject = new PackProject(dependencyContext.ProjectReferenceDependencyProvider, dependencyContext.ProjectResolver, libraryDescription);
if (packProject.Name == project.Name)
{
packProject.AppFolder = _options.AppFolder;
packProject.WwwRoot = _options.WwwRoot;
packProject.WwwRootOut = _options.WwwRootOut;
}
root.Projects.Add(packProject);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.Framework.PackageManager/Packing/PackOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ public class PackOptions

public string ProjectDir { get; set; }

public string AppFolder { get; set; }

public string Configuration { get; set; }

public string WwwRoot { get; set; }

public string WwwRootOut { get; set; }

public FrameworkName RuntimeTargetFramework { get; set; }

public bool Overwrite { get; set; }
Expand Down
56 changes: 37 additions & 19 deletions src/Microsoft.Framework.PackageManager/Packing/PackProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Security.Cryptography;
using Microsoft.Framework.Runtime;
using Newtonsoft.Json.Linq;
using NuGet;

namespace Microsoft.Framework.PackageManager.Packing
Expand All @@ -30,7 +31,8 @@ public PackProject(

public string Name { get { return _libraryDescription.Identity.Name; } }
public string TargetPath { get; private set; }
public string AppFolder { get; set; }
public string WwwRoot { get; set; }
public string WwwRootOut { get; set; }

public void EmitSource(PackRoot root)
{
Expand Down Expand Up @@ -70,6 +72,16 @@ public void EmitSource(PackRoot root)
return true;
});

// Update the 'webroot' property, which was specified with '--wwwroot-out' option
if (!string.IsNullOrEmpty(WwwRootOut))
{
var targetWebRootPath = Path.Combine(root.OutputPath, WwwRootOut);
var targetProjectJson = Path.Combine(TargetPath, Runtime.Project.ProjectFileName);
var jsonObj = JObject.Parse(File.ReadAllText(targetProjectJson));
jsonObj["webroot"] = PathUtility.GetRelativePath(targetProjectJson, targetWebRootPath);
File.WriteAllText(targetProjectJson, jsonObj.ToString());
}

// We can reference source files outside of project root with "code" property in project.json,
// e.g. { "code" : "..\\ExternalProject\\**.cs" }
// So we find out external source files and copy them separately
Expand Down Expand Up @@ -170,6 +182,12 @@ public void EmitNupkg(PackRoot root)

public void PostProcess(PackRoot root)
{
// If --wwwroot-out doesn't have a non-empty value, we don't need a public app folder in output
if (string.IsNullOrEmpty(WwwRootOut))
{
return;
}

Runtime.Project project;
if (!_projectResolver.TryResolveProject(_libraryDescription.Identity.Name, out project))
{
Expand All @@ -179,18 +197,17 @@ public void PostProcess(PackRoot root)
// Construct path to public app folder, which contains content files and tool dlls
// The name of public app folder is specified with "--appfolder" option
// Default name of public app folder is the same as main project
var appFolderName = AppFolder ?? PackRoot.DefaultAppFolderName;
var appFolderPath = Path.Combine(root.OutputPath, appFolderName);
var wwwRootOutPath = Path.Combine(root.OutputPath, WwwRootOut);

// Delete old public app folder because we don't want leftovers from previous operations
root.Operations.Delete(appFolderPath);
Directory.CreateDirectory(appFolderPath);
root.Operations.Delete(wwwRootOutPath);
Directory.CreateDirectory(wwwRootOutPath);

// Copy content files (e.g. html, js and images) of main project into public app folder
CopyContentFiles(root, project, appFolderName);
CopyContentFiles(root, project);

// Tool dlls including AspNet.Loader.dll go to bin folder under public app folder
var appFolderBinPath = Path.Combine(appFolderPath, "bin");
var wwwRootOutBinPath = Path.Combine(wwwRootOutPath, "bin");

var defaultRuntime = root.Runtimes.FirstOrDefault();
var iniFilePath = Path.Combine(TargetPath, "k.ini");
Expand All @@ -217,15 +234,15 @@ public void PostProcess(PackRoot root)
}

// Generate k.ini for public app folder
var appFolderIniFilePath = Path.Combine(appFolderPath, "k.ini");
var wwwRootOutIniFilePath = Path.Combine(wwwRootOutPath, "k.ini");
var appBaseLine = string.Format("KRE_APPBASE={0}",
Path.Combine("..", PackRoot.AppRootName, "src", project.Name));
var iniContents = string.Empty;
if (File.Exists(iniFilePath))
{
iniContents = File.ReadAllText(iniFilePath);
}
File.WriteAllText(appFolderIniFilePath,
File.WriteAllText(wwwRootOutIniFilePath,
string.Format("{0}{1}", iniContents, appBaseLine));

// Copy tools/*.dll into bin to support AspNet.Loader.dll
Expand All @@ -236,35 +253,36 @@ public void PostProcess(PackRoot root)
{
foreach (var packageToolFile in Directory.EnumerateFiles(packageToolsPath, "*.dll").Select(Path.GetFileName))
{
if (!Directory.Exists(appFolderBinPath))
if (!Directory.Exists(wwwRootOutBinPath))
{
Directory.CreateDirectory(appFolderBinPath);
Directory.CreateDirectory(wwwRootOutBinPath);
}

// Copy to bin folder under public app folder
File.Copy(
Path.Combine(packageToolsPath, packageToolFile),
Path.Combine(appFolderBinPath, packageToolFile),
Path.Combine(wwwRootOutBinPath, packageToolFile),
true);
}
}
}
}

private void CopyContentFiles(PackRoot root, Runtime.Project project, string appFolderName)
private void CopyContentFiles(PackRoot root, Runtime.Project project)
{
Console.WriteLine("Copying contents of project dependency {0} to {1}",
_libraryDescription.Identity.Name, appFolderName);
_libraryDescription.Identity.Name, WwwRootOut);

var appFolderPath = Path.Combine(root.OutputPath, appFolderName);
var wwwRootPath = Path.Combine(project.ProjectDirectory, WwwRoot);
var wwwRootOutPath = Path.Combine(root.OutputPath, WwwRootOut);

Console.WriteLine(" Source {0}", project.ProjectDirectory);
Console.WriteLine(" Target {0}", appFolderPath);
Console.WriteLine(" Source {0}", wwwRootPath);
Console.WriteLine(" Target {0}", wwwRootOutPath);

// A set of content files that should be copied
var contentFiles = new HashSet<string>(project.ContentFiles, StringComparer.OrdinalIgnoreCase);

root.Operations.Copy(project.ProjectDirectory, appFolderPath, (isRoot, filePath) =>
root.Operations.Copy(wwwRootPath, wwwRootOutPath, (isRoot, filePath) =>
{
// We always explore a directory
if (Directory.Exists(filePath))
Expand All @@ -274,7 +292,7 @@ private void CopyContentFiles(PackRoot root, Runtime.Project project, string app

var fileName = Path.GetFileName(filePath);
// Public app folder doesn't need project.json
if (string.Equals(fileName, "project.json", StringComparison.OrdinalIgnoreCase))
if (string.Equals(fileName, Runtime.Project.ProjectFileName, StringComparison.OrdinalIgnoreCase))
{
return false;
}
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.Framework.PackageManager/Packing/PackRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class PackRoot
{
private readonly Runtime.Project _project;
public static readonly string AppRootName = "approot";
public static readonly string DefaultAppFolderName = "public";

public PackRoot(Runtime.Project project, string outputPath, IServiceProvider hostServices)
{
Expand Down
10 changes: 7 additions & 3 deletions src/Microsoft.Framework.PackageManager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,13 @@ public int Main(string[] args)
CommandOptionType.NoValue);
var optionRuntime = c.Option("--runtime <KRE>", "Names or paths to KRE files to include",
CommandOptionType.MultipleValue);
var optionAppFolder = c.Option("--appfolder <NAME>",
"Determine the name of the application primary folder", CommandOptionType.SingleValue);
var optionNative = c.Option("--native", "Build and include native images. User must provide targeted CoreCLR runtime versions along with this option.",
CommandOptionType.NoValue);
var optionWwwRoot = c.Option("--wwwroot <NAME>", "Name of public folder in the project directory",
CommandOptionType.SingleValue);
var optionWwwRootOut = c.Option("--wwwroot-out <NAME>",
"Name of public folder in the packed image, can be used only when the '--wwwroot' option or 'webroot' in project.json is specified",
CommandOptionType.SingleValue);
c.HelpOption("-?|-h|--help");

c.OnExecute(() =>
Expand All @@ -118,9 +121,10 @@ public int Main(string[] args)
{
OutputDir = optionOut.Value(),
ProjectDir = argProject.Value ?? System.IO.Directory.GetCurrentDirectory(),
AppFolder = optionAppFolder.Value(),
Configuration = optionConfiguration.Value() ?? "Debug",
RuntimeTargetFramework = _environment.RuntimeFramework,
WwwRoot = optionWwwRoot.Value(),
WwwRootOut = optionWwwRootOut.Value() ?? optionWwwRoot.Value(),
Overwrite = optionOverwrite.HasValue(),
NoSource = optionNoSource.HasValue(),
Runtimes = optionRuntime.HasValue() ?
Expand Down