public static string applyVars(string val, string outputDir, Project project) { val = val.Replace("$author", project.Author); val = val.Replace("$version", project.Version); val = val.Replace("$output", outputDir); val = val.Replace("$projectName", project.Name); val = val.Replace("$projectDir", project.ProjectDir.FullName); //val.Replace("$fileName", fileName); return val; }
static public void Build(Project project) { try { SetProgress(1, "Initializing..."); string projectDir = Util.FixName(project.ProjectDir.FullName); string outputDir = Util.FixName(project.Output); string header = Util.applyVars(project.Copyright, outputDir, project); if (header.Length > 0) { header = "/*\r\n * " + header.Replace("\n", "\n * ") + "\r\n */\r\n\r\n"; } string build = Util.FixName(Util.applyVars(project.MinDir, outputDir, project)); string src = Util.FixName(Util.applyVars(project.SourceDir, outputDir, project)); string doc = Util.FixName(Util.applyVars(project.DocDir, outputDir, project)); SetProgress(10, "Loading source files..."); Dictionary<string, SourceFile> files = project.LoadSourceFiles(); int fileValue = 40 / files.Count, fileNumber = 0; foreach (SourceFile file in files.Values) { SetProgress(10 + (fileValue * ++fileNumber), "Compressing file " + (fileNumber) + " of " + files.Count); RaiseMessage(MessageTypes.StatusBar, "Processing " + file.File.FullName + "..."); file.Header = header; if (project.Source) { DirectoryInfo dir = getDirectory(src + file.PathInfo); file.CopyTo(Util.FixName(dir.FullName) + file.File.Name); } if (project.Minify) { DirectoryInfo buildDir = getDirectory(build + file.PathInfo); file.MinifyTo(Util.FixName(buildDir.FullName) + file.File.Name.Replace(".js", Options.Instance.OutputSuffix + ".js")); } //file.GetCommentBlocks(); } bool jsdocSkipped = false; if (project.Doc) { if (Options.Instance.JsdocPath.Length < 1 || !new FileInfo(Options.Instance.JsdocPath).Exists) { jsdocSkipped = true; } else { SetProgress(65, "Running JSDoc..."); string fileList = ""; foreach (SourceFile f in files.Values) { fileList += f.File.FullName + " "; } DirectoryInfo outDir = getDirectory(doc); string args = Options.Instance.JsdocPath + " " + Options.Instance.JsdocArgs.Replace("$docPath", outDir.FullName); args = args.Replace("$files", fileList); Process p = new Process(); p.EnableRaisingEvents = false; ProcessStartInfo info = new ProcessStartInfo("perl.exe", args); info.CreateNoWindow = true; info.UseShellExecute = false; p.StartInfo = info; try { p.Start(); p.WaitForExit(); } catch (Exception e) { jsdocSkipped = true; } } } SetProgress(70, "Loading build targets..."); List<Target> targets = project.GetTargets(true); if (targets.Count > 0) { int targetValue = 30 / targets.Count, targetNumber = 0; foreach (Target target in targets) { SetProgress(70 + (targetValue * ++targetNumber), "Building target " + (targetNumber) + " of " + targets.Count); FileInfo fi = new FileInfo(Util.applyVars(target.File, outputDir, project)); fi.Directory.Create(); StreamWriter sw = new StreamWriter(fi.FullName); sw.Write(header); if (!target.Shorthand) { foreach (string f in target.Includes) { sw.Write(files[f].Minified + "\n"); } } else { string[] sh = target.ParseList(); StringBuilder fcn = new StringBuilder(); fcn.Append("(function(){"); int index = 0; foreach (string s in sh) { fcn.AppendFormat("var _{0} = {1};", ++index, s); } sw.Write(fcn.Append("\n")); foreach (string f in target.Includes) { string min = files[f].Minified; index = 0; foreach (string s in sh) { min = min.Replace(s, "_" + index); } sw.Write(min + "\n"); } sw.Write("})();"); } sw.Close(); if (target.Debug) { StreamWriter dsw = new StreamWriter(fi.FullName.Substring(0, fi.FullName.Length - 3) + "-debug.js"); dsw.Write(header); foreach (string f in target.Includes) { dsw.Write("\r\n/*\r\n------------------------------------------------------------------\r\n"); dsw.Write("// File: " + files[f].PathInfo + "\\" + files[f].File.Name + "\r\n"); dsw.Write("------------------------------------------------------------------\r\n*/\r\n"); dsw.Write(files[f].Source + "\n"); } dsw.Close(); } } } if (jsdocSkipped) { RaiseMessage(MessageTypes.MessageBoxInfo, "JSDoc was skipped because the path to JSDoc (" + Options.Instance.JsdocPath + ") is invalid or perl.exe was not found. You can\nchange the path by selecting Build->Options and entering a new path."); } RaiseMessage(MessageTypes.StatusBar, "Build Complete."); SetProgress(100, "Done"); } catch (Exception ex) { RaiseMessage(MessageTypes.MessageBoxError, "The build has failed due to the following error:\n" + ex.Message); } }
static public void Build(Project project, string outputDir) { // NOTE: Make sure that we DO throw any errors that might occur here // and defer handling to the caller. The console app must be allowed // to catch errors so that it can exit unsuccessfully. SetProgress(1, "Initializing..."); string projectDir = Util.FixPath(project.ProjectDir.FullName); outputDir = outputDir != null ? Util.FixPath(outputDir) : Util.FixPath(project.Output); //Fix for default '$project\build' getting rendered literally: outputDir = outputDir.Replace("$project", projectDir); RaiseMessage(MessageTypes.Status, "Output path = " + outputDir); //rrs - option of clearing the output dir if (Options.GetInstance().ClearOutputDir) { RaiseMessage(MessageTypes.Status, "Clearing existing output..."); Util.ClearOutputDirectory(outputDir); } //rrs string header = Util.ApplyVars(project.Copyright, outputDir, project); if (header.Length > 0) { header = "/*\r\n * " + header.Replace("\n", "\n * ") + "\r\n */\r\n\r\n"; } string build = Util.FixPath(Util.ApplyVars(project.MinDir, outputDir, project)); string src = Util.FixPath(Util.ApplyVars(project.SourceDir, outputDir, project)); string doc = Util.FixPath(Util.ApplyVars(project.DocDir, outputDir, project)); SetProgress(10, "Loading source files..."); Dictionary<string, SourceFile> files = project.LoadSourceFiles(); float fileValue = 60.0f / (files.Count > 0 ? files.Count : 1); int fileNumber = 0; foreach (SourceFile file in files.Values) { int pct = (int)(fileValue * ++fileNumber); SetProgress(10 + pct, "Building file " + (fileNumber) + " of " + files.Count); RaiseMessage(MessageTypes.Status, "Processing " + file.File.FullName + "..."); file.Header = header; if (project.Source) { //Copy original source files to source output path DirectoryInfo dir = getDirectory(src + file.PathInfo); file.CopyTo(Util.FixPath(dir.FullName) + file.File.Name); } if (project.Minify && file.SupportsSourceParsing) { //Minify files and copy to build output path DirectoryInfo dir = getDirectory(build + file.PathInfo); file.MinifyTo(Util.FixPath(dir.FullName) + file.OutputFilename); } //file.GetCommentBlocks(); } bool jsdocSkipped = false; if (project.Doc) { if (Options.GetInstance().JsdocPath.Length < 1 || !new FileInfo(Options.GetInstance().JsdocPath).Exists) { jsdocSkipped = true; } else { SetProgress(75, "Creating JSDoc output..."); string fileList = ""; foreach (SourceFile f in files.Values) { if (f.SupportsSourceParsing) { fileList += f.File.FullName + " "; } } DirectoryInfo outDir = getDirectory(doc); string args = GetJsdocPath() + Options.GetInstance().JsdocArgs.Replace("$docPath", outDir.FullName); args = args.Replace("$files", fileList); Process p = new Process(); p.EnableRaisingEvents = false; ProcessStartInfo info = new ProcessStartInfo("perl.exe", args); info.CreateNoWindow = true; info.UseShellExecute = false; p.StartInfo = info; try { p.Start(); p.WaitForExit(); } catch (Exception e) { jsdocSkipped = true; } } } SetProgress(85, "Loading build targets..."); List<Target> targets = project.GetTargets(true); bool targetsSkipped = false; if (targets.Count > 0) { float targetValue = 10.0f / (targets.Count > 0 ? targets.Count : 1); int targetNumber = 0; foreach (Target target in targets) { if (target.Includes == null) { targetsSkipped = true; continue; } int pct = (int)(targetValue * ++targetNumber); SetProgress(85 + pct, "Building target " + (targetNumber) + " of " + targets.Count); FileInfo fi = new FileInfo(Util.ApplyVars(target.File, outputDir, project)); fi.Directory.Create(); StreamWriter sw = new StreamWriter(fi.FullName); sw.Write(header); if (!target.Shorthand) { foreach (string f in target.Includes) { sw.Write(files[f].Minified + "\n"); } } else { string[] sh = target.ParseList(); StringBuilder fcn = new StringBuilder(); fcn.Append("(function(){"); int index = 0; foreach (string s in sh) { fcn.AppendFormat("var _{0} = {1};", ++index, s); } sw.Write(fcn.Append("\n")); foreach (string f in target.Includes) { string min = files[f].Minified; index = 0; foreach (string s in sh) { min = min.Replace(s, "_" + index); } sw.Write(min + "\n"); } sw.Write("})();"); } sw.Close(); if (target.Debug) { string filename = fi.FullName; if (fi.Extension == ".js") { //Only rename to -debug for javascript files filename = fi.FullName.Substring(0, fi.FullName.Length - 3) + "-debug.js"; } StreamWriter dsw = new StreamWriter(filename); dsw.Write(header); foreach (string f in target.Includes) { //dsw.Write("\r\n/*\r\n------------------------------------------------------------------\r\n"); //dsw.Write("// File: " + files[f].PathInfo + "\\" + files[f].File.Name + "\r\n"); //dsw.Write("------------------------------------------------------------------\r\n*/\r\n"); dsw.Write(files[f].GetSourceNoComments() + "\n"); } dsw.Close(); } } } if (targetsSkipped) { RaiseMessage(MessageTypes.Info, "One or more build targets referenced files that are no longer included in the build project, so they were skipped."); } if (jsdocSkipped) { RaiseMessage(MessageTypes.Info, "JSDoc was skipped because the path to JSDoc (" + Options.GetInstance().JsdocPath + ") is invalid or perl.exe was not found.\nYou can change the path by selecting Build->Options and entering a new path."); } SetProgress(100, "Done"); if (BuildComplete != null) { BuildComplete(); } }
static public void Build(Project project) { Build(project, null); }