Skip to content
Open
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
77 changes: 73 additions & 4 deletions src/WebCompiler/Config/Config.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

Expand Down Expand Up @@ -27,6 +28,18 @@ public class Config
[JsonProperty("inputFile")]
public string InputFile { get; set; }

/// <summary>
/// The relative file path to the output files.
/// </summary>
[JsonProperty("outputFiles")]
public string OutputFiles { get; set; }

/// <summary>
/// The relative file path to the input files.
/// </summary>
[JsonProperty("inputFiles")]
public string InputFiles { get; set; }

/// <summary>
/// Settings for the minification.
/// </summary>
Expand Down Expand Up @@ -58,17 +71,30 @@ public class Config
/// </summary>
public FileInfo GetAbsoluteInputFile()
{
string folder = new FileInfo(FileName).DirectoryName;
return new FileInfo(Path.Combine(folder, InputFile.Replace("/", "\\")));
return new FileInfo(GetAbsolutePath(InputFile));
}

/// <summary>
/// Converts the relative output file to an absolute file path.
/// </summary>
public FileInfo GetAbsoluteOutputFile()
{
return new FileInfo(GetAbsolutePath(OutputFile));
}

private string GetAbsolutePath(string relativePath)
{
string folder = new FileInfo(FileName).DirectoryName;
return Path.Combine(folder, relativePath.Replace("/", "\\"));
}

private string GetRelativePath(string absolutePath)
{
if (absolutePath == null)
return null;

string folder = new FileInfo(FileName).DirectoryName;
return new FileInfo(Path.Combine(folder, OutputFile.Replace("/", "\\")));
return absolutePath.Replace(folder, string.Empty).TrimStart('\\').Replace("\\", "/");
}

/// <summary>
Expand All @@ -85,6 +111,49 @@ internal bool CompilationRequired()
return input.LastWriteTimeUtc > output.LastWriteTimeUtc;
}

internal IEnumerable<Config> ExpandConfig()
{
string inputFiles = InputFiles;
string outputFiles = OutputFiles;

InputFiles = OutputFiles = null;

if (!string.IsNullOrEmpty(InputFile) && !string.IsNullOrEmpty(OutputFile))
{
yield return this;
}

if (string.IsNullOrEmpty(inputFiles) || string.IsNullOrEmpty(outputFiles))
{
yield break;
}

inputFiles = GetAbsolutePath(inputFiles);
outputFiles = GetAbsolutePath(outputFiles);

string inputFolder = Path.GetDirectoryName(inputFiles);
string inputFilePattern = Path.GetFileName(inputFiles);
string outputFolder = Path.GetDirectoryName(outputFiles);
string outputFilePattern = Path.GetFileName(outputFiles);

foreach (var filePath in Directory.GetFiles(inputFolder, inputFilePattern))
{
var fileConfig = Clone();
fileConfig.InputFile = GetRelativePath(filePath);
fileConfig.OutputFile = GetRelativePath(Path.Combine(outputFolder,
Path.ChangeExtension(Path.GetFileName(filePath), Path.GetExtension(outputFilePattern))));
yield return fileConfig;
}
}

private Config Clone()
{
var clone = (Config)MemberwiseClone();
clone.Minify = Minify.AsEnumerable().ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
clone.Options = Options.AsEnumerable().ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
return clone;
}

/// <summary>
/// Determines if the object is equals to the other object.
/// </summary>
Expand Down
14 changes: 12 additions & 2 deletions src/WebCompiler/Config/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ConfigHandler
/// <param name="config">The compiler config object to add to the configration file.</param>
public void AddConfig(string fileName, Config config)
{
IEnumerable<Config> existing = GetConfigs(fileName);
IEnumerable<Config> existing = GetVerbatimConfigs(fileName);
List<Config> configs = new List<Config>();
configs.AddRange(existing);
configs.Add(config);
Expand All @@ -39,7 +39,7 @@ public void AddConfig(string fileName, Config config)
/// </summary>
public void RemoveConfig(Config configToRemove)
{
IEnumerable<Config> configs = GetConfigs(configToRemove.FileName);
IEnumerable<Config> configs = GetVerbatimConfigs(configToRemove.FileName);
List<Config> newConfigs = new List<Config>();

if (configs.Contains(configToRemove))
Expand Down Expand Up @@ -95,6 +95,16 @@ public void CreateDefaultsFile(string fileName)
/// <param name="fileName">A relative or absolute file path to the configuration file.</param>
/// <returns>A list of Config objects.</returns>
public static IEnumerable<Config> GetConfigs(string fileName)
{
IEnumerable<Config> configs = GetVerbatimConfigs(fileName);

if (configs != null && configs.Any())
configs = configs.SelectMany(c => c.ExpandConfig()).Distinct().ToList();

return configs;
}

private static IEnumerable<Config> GetVerbatimConfigs(string fileName)
{
FileInfo file = new FileInfo(fileName);

Expand Down
2 changes: 2 additions & 0 deletions src/WebCompilerTest/Config/ConfigHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public void AddConfig()
var newConfig = new WebCompiler.Config();
const string newInputFileName = "newInputFile";
newConfig.InputFile = newInputFileName;
const string newOutputFileName = "newOutputFile";
newConfig.OutputFile = newOutputFileName;

_handler.AddConfig(processingConfigFile, newConfig);

Expand Down
18 changes: 9 additions & 9 deletions src/WebCompilerVsix/JSON/compilerconfig-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,24 @@
},

"config": {
"required": [ "outputFile", "inputFile" ],

"properties": {
"properties": {
"includeInProject": {
"description": "Set to true to include the output file in the project. Doesn't work in some Visual Studio project types like ASP.NET 5 applications.",
"type": "boolean",
"default": true
},
"inputFiles": { "type": "string" },
"inputFile": {
"description": "One or more relative file names to bundle.",
"type": "string",
"format": "compiler_relativepath"
"description": "One or more relative file names to bundle.",
"type": "string",
"format": "compiler_relativepath"
},
"minify": {
"description": "Specify options for minification of the output file.",
"type": "object",
"allOf": [ { "$ref": "compilerdefaults-schema.json#/definitions/baseMinify" } ]
"description": "Specify options for minification of the output file.",
"type": "object",
"allOf": [ { "$ref": "compilerdefaults-schema.json#/definitions/baseMinify" } ]
},
"outputFiles": { "type": "string" },
"outputFile": {
"description": "The relative path to the desired output file name.",
"type": "string",
Expand Down