|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | + |
| 7 | +namespace WebCompiler |
| 8 | +{ |
| 9 | + class HandlebarsCompiler : ICompiler |
| 10 | + { |
| 11 | + private static Regex _errorRx = new Regex("Error: (?<message>.+) on line (?<line>[0-9]+):", RegexOptions.Compiled); |
| 12 | + private string _mapPath; |
| 13 | + private string _path; |
| 14 | + private string _name = string.Empty; |
| 15 | + private string _extension = string.Empty; |
| 16 | + private string _output = string.Empty; |
| 17 | + private string _error = string.Empty; |
| 18 | + private bool _partial = false; |
| 19 | + |
| 20 | + public HandlebarsCompiler(string path) |
| 21 | + { |
| 22 | + _path = path; |
| 23 | + } |
| 24 | + |
| 25 | + public CompilerResult Compile(Config config) |
| 26 | + { |
| 27 | + string baseFolder = Path.GetDirectoryName(config.FileName); |
| 28 | + string inputFile = Path.Combine(baseFolder, config.InputFile); |
| 29 | + |
| 30 | + FileInfo info = new FileInfo(inputFile); |
| 31 | + string content = File.ReadAllText(info.FullName); |
| 32 | + |
| 33 | + CompilerResult result = new CompilerResult |
| 34 | + { |
| 35 | + FileName = info.FullName, |
| 36 | + OriginalContent = content, |
| 37 | + }; |
| 38 | + |
| 39 | + var extension = Path.GetExtension(inputFile); |
| 40 | + if (!string.IsNullOrWhiteSpace(extension)) |
| 41 | + { |
| 42 | + _extension = extension.Substring(1); |
| 43 | + } |
| 44 | + |
| 45 | + var name = Path.GetFileNameWithoutExtension(inputFile); |
| 46 | + if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("_")) |
| 47 | + { |
| 48 | + _name = name.Substring(1); |
| 49 | + _partial = true; |
| 50 | + |
| 51 | + // Temporarily Fix |
| 52 | + // TODO: Remove after actual fix |
| 53 | + var tempFilename = Path.Combine(Path.GetDirectoryName(inputFile), _name + ".handlebarstemp"); |
| 54 | + info.CopyTo(tempFilename); |
| 55 | + info = new FileInfo(tempFilename); |
| 56 | + _extension = "handlebarstemp"; |
| 57 | + } |
| 58 | + |
| 59 | + _mapPath = Path.ChangeExtension(inputFile, ".js.map.tmp"); |
| 60 | + |
| 61 | + try |
| 62 | + { |
| 63 | + RunCompilerProcess(config, info); |
| 64 | + |
| 65 | + result.CompiledContent = _output; |
| 66 | + |
| 67 | + var options = HandlebarsOptions.FromConfig(config); |
| 68 | + if (options.SourceMap || config.SourceMap) |
| 69 | + { |
| 70 | + if (File.Exists(_mapPath)) |
| 71 | + result.SourceMap = File.ReadAllText(_mapPath); |
| 72 | + } |
| 73 | + |
| 74 | + if (_error.Length > 0) |
| 75 | + { |
| 76 | + CompilerError ce = new CompilerError |
| 77 | + { |
| 78 | + FileName = inputFile, |
| 79 | + Message = _error.Replace(baseFolder, string.Empty), |
| 80 | + IsWarning = !string.IsNullOrEmpty(_output) |
| 81 | + }; |
| 82 | + |
| 83 | + var match = _errorRx.Match(_error); |
| 84 | + |
| 85 | + if (match.Success) |
| 86 | + { |
| 87 | + ce.Message = match.Groups["message"].Value.Replace(baseFolder, string.Empty); |
| 88 | + ce.LineNumber = int.Parse(match.Groups["line"].Value); |
| 89 | + ce.ColumnNumber = 0; |
| 90 | + } |
| 91 | + |
| 92 | + result.Errors.Add(ce); |
| 93 | + } |
| 94 | + } |
| 95 | + catch (Exception ex) |
| 96 | + { |
| 97 | + CompilerError error = new CompilerError |
| 98 | + { |
| 99 | + FileName = inputFile, |
| 100 | + Message = string.IsNullOrEmpty(_error) ? ex.Message : _error, |
| 101 | + LineNumber = 0, |
| 102 | + ColumnNumber = 0, |
| 103 | + }; |
| 104 | + |
| 105 | + result.Errors.Add(error); |
| 106 | + } |
| 107 | + finally |
| 108 | + { |
| 109 | + if (File.Exists(_mapPath)) |
| 110 | + { |
| 111 | + File.Delete(_mapPath); |
| 112 | + } |
| 113 | + // Temporarily Fix |
| 114 | + // TODO: Remove after actual fix |
| 115 | + if (info.Extension == ".handlebarstemp") |
| 116 | + { |
| 117 | + info.Delete(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return result; |
| 122 | + } |
| 123 | + |
| 124 | + private void RunCompilerProcess(Config config, FileInfo info) |
| 125 | + { |
| 126 | + string arguments = ConstructArguments(config); |
| 127 | + |
| 128 | + ProcessStartInfo start = new ProcessStartInfo |
| 129 | + { |
| 130 | + WorkingDirectory = info.Directory.FullName, |
| 131 | + UseShellExecute = false, |
| 132 | + WindowStyle = ProcessWindowStyle.Hidden, |
| 133 | + CreateNoWindow = true, |
| 134 | + FileName = "cmd.exe", |
| 135 | + Arguments = $"/c \"\"{Path.Combine(_path, "node_modules\\.bin\\handlebars.cmd")}\" \"{info.FullName}\" {arguments}\"", |
| 136 | + StandardOutputEncoding = Encoding.UTF8, |
| 137 | + StandardErrorEncoding = Encoding.UTF8, |
| 138 | + RedirectStandardOutput = true, |
| 139 | + RedirectStandardError = true, |
| 140 | + }; |
| 141 | + |
| 142 | + start.EnvironmentVariables["PATH"] = _path + ";" + start.EnvironmentVariables["PATH"]; |
| 143 | + |
| 144 | + using (Process p = Process.Start(start)) |
| 145 | + { |
| 146 | + var stdout = p.StandardOutput.ReadToEndAsync(); |
| 147 | + var stderr = p.StandardError.ReadToEndAsync(); |
| 148 | + p.WaitForExit(); |
| 149 | + |
| 150 | + _output = stdout.Result.Trim(); |
| 151 | + _error = stderr.Result.Trim(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private string ConstructArguments(Config config) |
| 156 | + { |
| 157 | + string arguments = ""; |
| 158 | + |
| 159 | + HandlebarsOptions options = HandlebarsOptions.FromConfig(config); |
| 160 | + |
| 161 | + if (options.AMD) |
| 162 | + arguments += " --amd"; |
| 163 | + else if (!string.IsNullOrEmpty(options.CommonJS)) |
| 164 | + arguments += $" --commonjs \"{options.CommonJS}\""; |
| 165 | + |
| 166 | + foreach (var knownHelper in options.KnownHelpers) |
| 167 | + { |
| 168 | + arguments += $" --known \"{knownHelper}\""; |
| 169 | + } |
| 170 | + |
| 171 | + if (options.KnownHelpersOnly) |
| 172 | + arguments += " --knownOnly"; |
| 173 | + |
| 174 | + if (options.ForcePartial || _partial) |
| 175 | + arguments += " --partial"; |
| 176 | + |
| 177 | + if (options.NoBOM) |
| 178 | + arguments += " --bom"; |
| 179 | + |
| 180 | + if ((options.SourceMap || config.SourceMap) && !string.IsNullOrWhiteSpace(_mapPath)) |
| 181 | + arguments += $" --map \"{_mapPath}\""; |
| 182 | + |
| 183 | + if (!string.IsNullOrEmpty(options.TemplateNameSpace)) |
| 184 | + arguments += $" --namespace \"{options.TemplateNameSpace}\""; |
| 185 | + |
| 186 | + if (!string.IsNullOrEmpty(options.Root)) |
| 187 | + arguments += $" --root \"{options.Root}\""; |
| 188 | + |
| 189 | + if (!string.IsNullOrEmpty(options.Name)) |
| 190 | + arguments += $" --name \"{options.Name}\""; |
| 191 | + else if (!string.IsNullOrEmpty(_name)) |
| 192 | + arguments += $" --name \"{_name}\""; |
| 193 | + |
| 194 | + if (!string.IsNullOrEmpty(_extension)) |
| 195 | + arguments += $" --extension \"{_extension}\""; |
| 196 | + |
| 197 | + return arguments; |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments