public static CrakeFile Parse(string fileName) { if (!File.Exists(fileName)) { throw new FileNotFoundException("Crakefile '{0}' not found!", fileName); } var crakeFile = new CrakeFile(); crakeFile.Name = Path.GetFileName(fileName); var file = new StreamReader(File.OpenRead(fileName)); try { var line = file.ReadLine(); while (line != null) { line = MatchKeyword(crakeFile, ref file, line); if (string.IsNullOrEmpty(line) || line == "\n") { line = file.ReadLine(); } } } finally { file.Dispose(); } foreach (var dependency in crakeFile.Dependencies) { dependency.Resolve(); } return crakeFile; }
public void ShouldParseImports2() { var crakeFile = new CrakeFile(); var file = new StreamReader(new MemoryStream()); var actual = imports.Parse(crakeFile, ref file, "System.Web"); Assert.AreEqual(null, actual); Assert.AreEqual("System.Web", crakeFile.Imports[0]); }
public void ShouldParseAssemblyDependency() { var crakeFile = new CrakeFile(); var file = new StreamReader(new MemoryStream()); var actual = require.Parse(crakeFile, ref file, "System"); Assert.AreEqual(null, actual); Assert.IsInstanceOf<AssemblyDependency>(crakeFile.Dependencies[0]); Assert.AreEqual("System", ((AssemblyDependency)crakeFile.Dependencies[0]).AssemblyName); }
public void CompileParsedCrakeFile(CrakeFile crakeFile) { var type = new CodeTypeDeclaration(crakeFile.UniqueName); type.IsClass = true; type.Attributes = MemberAttributes.Public | MemberAttributes.Static; foreach (var item in crakeFile.Imports) { codeNamespace.Imports.Add(new CodeNamespaceImport(item)); } foreach (var plainTask in crakeFile.Tasks) { var task = new CodeMemberMethod(); task.Attributes = MemberAttributes.Public | MemberAttributes.Static; task.Name = plainTask.Name.Replace(":", "_"); task.Attributes = MemberAttributes.Public | MemberAttributes.Static; task.Statements.Add(new CodeSnippetStatement(plainTask.Body)); type.Members.Add(task); } if (crakeFile.HelperMethods.Count > 0) { var helperMethods = new CodeTypeDeclaration("HelperMethods"); helperMethods.IsClass = true; helperMethods.Attributes = MemberAttributes.Public | MemberAttributes.Static; foreach (var helperMethod in crakeFile.HelperMethods) { var method = new CodeMemberMethod(); method.ReturnType = new CodeTypeReference(helperMethod.Return.ToString()); method.Name = helperMethod.Name.ToString(); method.Attributes = MemberAttributes.Public | MemberAttributes.Static; method.Statements.Add(new CodeSnippetStatement(helperMethod.Body.ToString())); if (helperMethod.Params.ToString().Trim() != string.Empty) { foreach (var param in helperMethod.Params.ToString().Split(',')) { var p = param.Split(' '); method.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(p[0]), p[1])); } } helperMethods.Members.Add(method); } type.Members.Add(helperMethods); } foreach (var r in crakeFile.Dependencies) { if (r is AssemblyDependency) { var name = ((AssemblyDependency)r).Location; if (!require.Contains(name)) require.Add(name); } } codeNamespace.Types.Add(type); if (!codeUnit.Namespaces.Contains(codeNamespace)) codeUnit.Namespaces.Add(codeNamespace); }
public void ShouldThrowSyntaxError() { var crakeFile = new CrakeFile(); var file = new StreamReader(new MemoryStream()); imports.Parse(crakeFile, ref file, ""); }
private void ResolveCalls(CrakeFile crakeFile, MethodHelper method) { foreach (var task in crakeFile.Tasks) { task.Body = Regex.Replace(task.Body, string.Format("\\b{0}\\s*\\(", method.Name), "HelperMethods.$0", RegexOptions.Multiline); } var body = method.Body.ToString(); method.Body.Remove(0, method.Body.Length); method.Body.Append(Regex.Replace(body, "\\bPARAMS\\s*\\[[^\\]]+\\]", "CrakeParameters.$0", RegexOptions.Multiline)); }
public void ShouldThrowSyntaxError3() { var crakeFile = new CrakeFile(); var file = new StreamReader(new MemoryStream()); require.Parse(crakeFile, ref file, "\"System"); }
public void ShouldValidateSyntax9() { var file = new StreamReader (new MemoryStream ()); var parent = new CrakeFile (); keyword.Parse (parent, ref file, ":name1", "do", ""); }
public void ShouldValidateSyntax10() { var file = new StreamReader (new MemoryStream ()); var parent = new CrakeFile (); var actual = keyword.Parse (parent, ref file, ":name1", "do", "end"); Assert.IsNullOrEmpty(actual); }
public void ShouldNotMatch() { var file = new StreamReader (new MemoryStream ()); var parent = new CrakeFile (); var actual = keyword.IsMatch (parent, ref file, "namespaces :name1 do"); Assert.IsNull (actual); }