public override bool Execute() { CompileEngine engine = new CompileEngine(); CompileOptions options = new CompileOptions(_singleFile ? ArtifactsType.SingleFile : ArtifactsType.MultipleFile, _build); options.GenerateSourceMap = this._generateSourceMap; engine.InitializeCompiler(this._csprojPath, options, _supressWarnings); var watch = Stopwatch.StartNew(); var issues = engine.Compile(); watch.Stop(); Log.LogMessage(" {0} Compile done in {1} ms.", Constants.SharpJs, watch.ElapsedMilliseconds); foreach (var issue in issues.Issues) { var loc = issue.Location; string code = string.Format("{0}-{1}", Constants.SharpJs, issue.IssueId.ToString("0000")); switch (issue.IssueType) { case IssueType.Warning: Log.LogWarning(Constants.SharpJs, code, null, loc.FileName, loc.Line, loc.Column, loc.EndLine, loc.EndColumn, issue.Message); break; case IssueType.Error: Log.LogError(Constants.SharpJs, code, null, loc.FileName, loc.Line, loc.Column, loc.EndLine, loc.EndColumn, issue.Message); break; } } return !issues.HasError; }
public override bool Execute() { CompileEngine engine = new CompileEngine(); CompileOptions options = new CompileOptions(_singleFile ? ArtifactsType.SingleFile : ArtifactsType.MultipleFile, _build); options.GenerateSourceMap = this._generateSourceMap; engine.InitializeCompiler(this._csprojPath, options, _supressWarnings); var watch = Stopwatch.StartNew(); var issues = engine.Compile(); watch.Stop(); Log.LogMessage(" {0} Compile done in {1} ms.", Constants.SharpJs, watch.ElapsedMilliseconds); foreach (var issue in issues.Issues) { var loc = issue.Location; string code = string.Format("{0}-{1}", Constants.SharpJs, issue.IssueId.ToString("0000")); switch (issue.IssueType) { case IssueType.Warning: Log.LogWarning(Constants.SharpJs, code, null, loc.FileName, loc.Line, loc.Column, loc.EndLine, loc.EndColumn, issue.Message); break; case IssueType.Error: Log.LogError(Constants.SharpJs, code, null, loc.FileName, loc.Line, loc.Column, loc.EndLine, loc.EndColumn, issue.Message); break; } } return(!issues.HasError); }
public void InitializeCompiler(string csprojPath, CompileOptions options = null, string supressWarnings = "") { _options = (options != null) ? options : CompileOptions.DEFAULT; _options.StdError.Information($"{Constants.SharpJs} compile: project: {Path.GetFileNameWithoutExtension(csprojPath)}, configuration: {options.ProjectConfig}", true); string[] numbers = supressWarnings.Split(',', StringSplitOptions.RemoveEmptyEntries); var q = from n in numbers select int.Parse(n); _aggregator = new ErrorAggregator(q); #region Parse C# project file _startPath = Path.GetDirectoryName(csprojPath); XDoc doc = XDoc.LoadFromFile(csprojPath); doc.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003"); _outputScript = doc.GetStringValue(@"x:PropertyGroup/x:AssemblyName") + ArtifactsFactory.JavaScriptFileExtension; var cfgList = doc.NavigateToList(@"x:PropertyGroup[@Condition]"); bool configExist = false; foreach (var cfg in cfgList) { string condition = cfg.GetStringValue("@Condition"); if (condition.Contains(options.ProjectConfig)) { var outpath = cfg.GetStringValue("x:OutputPath"); _outputDir = Path.Combine(_startPath, outpath); configExist = true; break; } } if (!configExist) { _aggregator.AppendIssue(CompileIssue.CreateNoLocationIssue(IssueType.Error, IssueId.BuildConfigError, options.ProjectConfig)); return; } //Note:Project ref, get the assembly name and should be able to found under output var rl = doc.NavigateToList(@"x:ItemGroup/x:ProjectReference"); foreach (var r in rl) { var refPrjPath = r.GetStringValue(@"@Include"); XDoc xref = XDoc.LoadFromFile(Path.Combine(_startPath, refPrjPath)); xref.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003"); var libName = xref.GetStringValue(@"x:PropertyGroup/x:AssemblyName"); string assemblyPath = Path.GetFullPath(Path.Combine(_outputDir, libName + ".dll")); _refPathList.Add(assemblyPath); CheckAssembly(assemblyPath); } rl = doc.NavigateToList(@"x:ItemGroup/x:Reference"); foreach (var r in rl) { string hint = r.GetStringValue(@"x:HintPath"); if (!string.IsNullOrEmpty(hint))//Note: Add external assembly. { string assemblyPath = Path.GetFullPath(Path.Combine(_startPath, hint)); _refPathList.Add(assemblyPath); if (!CheckAssembly(assemblyPath)) { var issue = CompileIssue.CreateNoLocationIssue(IssueType.Warning, IssueId.InvalidAssembly, assemblyPath); _aggregator.AppendIssue(issue); } } } //Note:Item to be compiled. var cl = doc.NavigateToList(@"x:ItemGroup/x:Compile"); foreach (var c in cl) { string path = c.GetStringValue("@Include"); if (path == @"Properties\AssemblyInfo.cs") { continue; } _compilePathList.Add(Path.Combine(_startPath, path)); } _template = CodeTemplate.Parse(Path.Combine(_startPath, @"Properties\template.xml")); #endregion }
public void InitializeCompiler(string csprojPath, CompileOptions options = null, string supressWarnings = "") { _options = (options != null) ? options : CompileOptions.DEFAULT; _options.StdError.Information($"{Constants.SharpJs} compile: project: {Path.GetFileNameWithoutExtension(csprojPath)}, configuration: {options.ProjectConfig}", true); string[] numbers = supressWarnings.Split(',', StringSplitOptions.RemoveEmptyEntries); var q = from n in numbers select int.Parse(n); _aggregator = new ErrorAggregator(q); #region Parse C# project file _startPath = Path.GetDirectoryName(csprojPath); XDoc doc = XDoc.LoadFromFile(csprojPath); doc.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003"); _outputScript = doc.GetStringValue(@"x:PropertyGroup/x:AssemblyName") + ArtifactsFactory.JavaScriptFileExtension; var cfgList = doc.NavigateToList(@"x:PropertyGroup[@Condition]"); bool configExist = false; foreach (var cfg in cfgList) { string condition = cfg.GetStringValue("@Condition"); if (condition.Contains(options.ProjectConfig)) { var outpath = cfg.GetStringValue("x:OutputPath"); _outputDir = Path.Combine(_startPath, outpath); configExist = true; break; } } if (!configExist) { _aggregator.AppendIssue(CompileIssue.CreateNoLocationIssue(IssueType.Error, IssueId.BuildConfigError, options.ProjectConfig)); return; } //Note:Project ref, get the assembly name and should be able to found under output var rl = doc.NavigateToList(@"x:ItemGroup/x:ProjectReference"); foreach (var r in rl) { var refPrjPath = r.GetStringValue(@"@Include"); XDoc xref = XDoc.LoadFromFile(Path.Combine(_startPath, refPrjPath)); xref.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003"); var libName = xref.GetStringValue(@"x:PropertyGroup/x:AssemblyName"); string assemblyPath = Path.GetFullPath(Path.Combine(_outputDir, libName + ".dll")); _refPathList.Add(assemblyPath); CheckAssembly(assemblyPath); } rl = doc.NavigateToList(@"x:ItemGroup/x:Reference"); foreach (var r in rl) { string hint = r.GetStringValue(@"x:HintPath"); if (!string.IsNullOrEmpty(hint))//Note: Add external assembly. { string assemblyPath = Path.GetFullPath(Path.Combine(_startPath, hint)); _refPathList.Add(assemblyPath); if (!CheckAssembly(assemblyPath)) { var issue = CompileIssue.CreateNoLocationIssue(IssueType.Warning, IssueId.InvalidAssembly, assemblyPath); _aggregator.AppendIssue(issue); } } } //Note:Item to be compiled. var cl = doc.NavigateToList(@"x:ItemGroup/x:Compile"); foreach (var c in cl) { string path = c.GetStringValue("@Include"); if (path == @"Properties\AssemblyInfo.cs") continue; _compilePathList.Add(Path.Combine(_startPath, path)); } _template = CodeTemplate.Parse(Path.Combine(_startPath, @"Properties\template.xml")); #endregion }
static void Main(string[] args) { if (!ExtractParams(args)) { ShowMessage(); return; } else { CompileEngine engine = new CompileEngine(); ArtifactsType artifactsType = ArtifactsType.Console; if (_writeFile) { if (_singleFile) artifactsType = ArtifactsType.SingleFile; else artifactsType = ArtifactsType.MultipleFile; } if ((!string.IsNullOrEmpty(_config)) && _config.ToUpper() == "RELEASE") _config = CompileOptions.RELEASE_ANY_CPU; else _config = CompileOptions.DEBUG_ANY_CPU; CompileOptions opts = new CompileOptions(artifactsType, _config); opts.GenerateSourceMap = _generateSourceMap; engine.InitializeCompiler(_csprojFile, opts, _suppressWarnings); var errors = engine.Compile(); errors.WriteToStdErr(opts.StdError); } System.Console.ReadLine(); }