public JsFile GetJsFileFromCodeSnippet(string codeSnippet, SupportedLanguage language) { var file = new JsFile(); var snippetParser = new SnippetParser(language); var jsFile = new JsFile { FullPath = string.Empty }; var parsedNode = snippetParser.Parse(codeSnippet); if (parsedNode.Children.Count > 0) { var visitor = new AstVisitor { Model = jsFile }; parsedNode.AcceptVisitor(visitor, null); file = visitor.Model; return(file); } return(null); }
public JsFile GetJsFileFromCodeFile(string path, SupportedLanguage language) { TextReader textReader = File.OpenText(path); var file = new JsFile(); using (var parser = ParserFactory.CreateParser(language, textReader)) { var jsFile = new JsFile { FullPath = path }; parser.Parse(); if (parser.Errors.Count <= 0) { var visitor = new AstVisitor { Model = jsFile }; parser.CompilationUnit.AcceptVisitor(visitor, null); file = visitor.Model; return(file); } } return(null); }
public void AddUpdate(Endpoint endpoint, JsFile jsFile, List <ChangeDescriptor> changes) { try { if (!_entries.ContainsKey(endpoint.Pedigree)) { _entries.Add(endpoint.Pedigree, new Dictionary <string, List <ChangeDescriptor> >()); } var epEntry = _entries[endpoint.Pedigree]; var jsFileKey = $"{jsFile.Filename.ToLower()}.{jsFile.Version}"; if (!epEntry.ContainsKey(jsFileKey)) { epEntry.Add(jsFileKey, new List <ChangeDescriptor>()); } // add unique entries epEntry[jsFileKey].AddRange(changes.Where(c => !epEntry[jsFileKey].Exists(existing => existing.Description.Equals(c.Description, StringComparison.OrdinalIgnoreCase)))); } catch (Exception ex) { SessionLog.Exception(ex); } }
void LoadJsFile(string fileName) { try { jsFile = new JsFile(); if (string.IsNullOrEmpty(fileName)) { toolStripButtonJsFileSelect.Text = "..."; Properties.Settings.Default.JsFile = fileName; Properties.Settings.Default.Save(); return; } var hosObjects = new Dictionary <string, object> { { "Console", jsConsole }, { "Sys", jsSys }, { "Tags", tags }, }; jsFile.Load(fileName, hosObjects); toolStripButtonJsFileSelect.Text = fileName; Properties.Settings.Default.JsFile = fileName; Properties.Settings.Default.Save(); toolStripButtonRunLoop.Enabled = true; toolStripButtonRemoveJs.Enabled = true; } catch (Exception ex) { jsConsole.error(ex); toolStripButtonJsFileSelect.Text = "..."; toolStripButtonRunLoop.Enabled = false; toolStripButtonRemoveJs.Enabled = false; } }
public static ViewModel MapViewModelFromJsFile(JsFile jsFile) { var viewModel = new ViewModel { FullPath = jsFile.FullPath, Classes = new ObservableCollection <JsClassViewModel>(), CamelCase = true }; foreach (var file in jsFile.Files) { var jsClass = new JsClassViewModel(viewModel) { Name = file.Name, Properties = new ObservableCollection <JsPropertyViewModel>(), Ignore = false, Enable = false }; file.Properties.ForEach(p => jsClass.Properties.Add(new JsPropertyViewModel(jsClass) { Ignore = p.Ignore, Name = p.Name, OfType = p.OfType, IsArray = p.IsArray, IsObservable = p.IsObservable })); viewModel.Classes.Add(jsClass); } return(viewModel); }
public ApiResponse UpdateRule([FromQuery] string project, [FromQuery(Name = "app")] string appName, [FromQuery] string file, [FromRoute] string ruleId, [FromBody] Newtonsoft.Json.Linq.JObject json) { try { if (!ControllerHelper.GetProjectAndApp(project, appName, out var proj, out var app, out var resp)) { return(resp); } var type = (RuleType)int.Parse(json["Type"].ToString()); string value = json["Value"].ToString(); CommonReturnValue ret; JsFile jsFile = null; if (file == null) { // DB-level ret = app.UpdateRule(ruleId, value); } else { jsFile = app.GetJsFile(file); // TODO: Move check and error message down to App api? if (jsFile == null) { return(ApiResponse.ExclamationModal("The specified output file was not found.")); } ret = jsFile.UpdateRule(ruleId, value); } if (ret.IsSuccess) { SettingsInstance.SaveSettingsToFile(); if (jsFile == null) { WorkSpawner.SetRulesDirty(app); } else { WorkSpawner.SetRulesDirty(app, jsFile); } return(ApiResponse.Success()); } else { return(ApiResponse.ExclamationModal(ret.userErrorVal)); } } catch (Exception ex) { return(ApiResponse.Exception(ex)); } }
private static void CreateCrushedFiles(IPathProvider pathProvider, CssGroupElementCollection cssGroups, JsGroupElementCollection jsGroups, CssCrusher cssCrusher, JsCrusher jsCrusher) { _cssOutput = "Css Files created:\r\n"; foreach (CssGroupElement group in cssGroups) { var files = new List <CssFile>(); foreach (CssFileElement cssFile in group.Files) { var file = new CssFile() { CompressionType = cssFile.CompressionType, FilePath = cssFile.FilePath }; files.Add(file); } var outputUri = new Uri(pathProvider.ToAbsolute(group.OutputFilePath), UriKind.Relative); cssCrusher.AddFiles(outputUri, files, group.AppendHashToCssAsset); _cssOutput += outputUri + " (" + group.Name + ")\r\n"; foreach (var cssFile in files) { outputUri = new Uri(pathProvider.ToAbsolute(cssFile.FilePath), UriKind.Relative); _cssOutput += " " + outputUri + "\r\n"; } } _jsOutput = "Js Files created:\r\n"; foreach (JsGroupElement group in jsGroups) { var files = new List <JsFile>(); foreach (JsFileElement cssFile in group.Files) { var file = new JsFile() { CompressionType = cssFile.CompressionType, FilePath = cssFile.FilePath }; files.Add(file); } var outputUri = new Uri(pathProvider.ToAbsolute(group.OutputFilePath), UriKind.Relative); jsCrusher.AddFiles(outputUri, files); _jsOutput += outputUri + " (" + group.Name + ")\r\n"; foreach (var jsFile in files) { outputUri = new Uri(pathProvider.ToAbsolute(jsFile.FilePath), UriKind.Relative); _jsOutput += " " + outputUri + "\r\n"; } } }
public static void SetRulesDirty(Application app, JsFile jsFile) { // regen only affected jsFile var worker = WorkSpawner._workerList.FirstOrDefault(w => w.Endpoint.Application == app && app.JsFiles.Contains(jsFile)); if (worker != null) { worker.QueueInstruction(new WorkerInstruction() { Type = WorkerInstructionType.RegenSpecificFile, JsFile = jsFile }); } }
private void ForceGenerateOutputFile(Endpoint endpoint, JsFile jsFile) { try { JsFileGenerator.GenerateJsFileV2("003", endpoint, jsFile, rulesChanged: true); this.IsOutputFilesDirty = false; endpoint.LastUpdateDate = DateTime.Now; } catch (Exception ex) { this.log.Exception(ex); } }
public override object VisitTypeDeclaration(TypeDeclaration typeDeclaration, object data) { Contract.Requires(typeDeclaration != null); // Is this a class but not a test fixture? if (IsClass(typeDeclaration) && !HasTestFixtureAttribute(typeDeclaration)) { var parent = new JsClass() { Name = typeDeclaration.Name, Properties = new List <JsProperty>() }; if (typeDeclaration.Attributes.Count > 0) { foreach (var attribute in typeDeclaration.Attributes) { var jsAttributes = new List <JsAttribute>(); var a = attribute.Attributes[0]; jsAttributes.Add(new JsAttribute() { Name = a.Name }); parent.Attributes = jsAttributes; } } if (Model == null) { Model = new JsFile(); } if (Model.Files == null) { Model.Files = new List <JsClass>(); } Model.Files.Add(parent); CurrentParent = parent; } // Visit children (E.g. MethodDeclarion objects) typeDeclaration.AcceptChildren(this, data); return(null); }
public static string Convert(JsFile t) { MemberInfo memberInfo = typeof(JsFile).GetMember(t.ToString()) .FirstOrDefault(); if (memberInfo != null) { JsFilenameAttribute attribute = (JsFilenameAttribute) memberInfo.GetCustomAttributes(typeof(JsFilenameAttribute), false) .FirstOrDefault(); return(attribute?.Filename); } return(null); }
public int CountChanges(Endpoint endpoint, JsFile jsFile, int fromVersion, int toVersion, string filterJson) { // TODO: take filterJson into account if (!_entries.ContainsKey(endpoint.Pedigree)) { return(0); } var epEntry = _entries[endpoint.Pedigree]; int totalChanges = 0; string changedByFilter = null; Regex changedByRegex = null; if (filterJson != null) { var filter = JsonConvert.DeserializeObject <dynamic>(filterJson); if (filter["changedBy"] != null) { changedByFilter = filter["changedBy"]; changedByRegex = new Regex(changedByFilter); } } for (var i = fromVersion + 1; i <= toVersion; i++) { var jsFileKey = $"{jsFile.Filename.ToLower()}.{i}"; if (!epEntry.ContainsKey(jsFileKey) || epEntry[jsFileKey].Count == 0) { continue; } totalChanges += epEntry[jsFileKey].Count(chg => { if (string.IsNullOrWhiteSpace(changedByFilter)) { return(true); } return(changedByRegex.IsMatch(chg.ChangedBy)); }); } return(totalChanges); }
public void Process() { List <ITypeDefinition> allTypesToExport = GetAllTypesToExport(); var byFile = allTypesToExport.GroupBy(ce => Compiler.PathMerger.ConvertRelativePath(Sk.GetExportPath(ce))).ToDictionary(); byFile.ForEach(t => SortByNativeInheritance(t.Value)); foreach (var f in byFile) { var customOrder = f.Value.Where(t => GetOrderInFile(t) != 0).ToList(); if (customOrder.Count > 0) { f.Value.RemoveAll(t => customOrder.Contains(t)); customOrder.Sort((x, y) => GetOrderInFile(x) - GetOrderInFile(y)); f.Value.InsertRange(0, customOrder.Where(t => GetOrderInFile(t) < 0)); f.Value.AddRange(customOrder.Where(t => GetOrderInFile(t) > 0)); } } //sort types by OrderInFile if needed: //byFile.Where(k => k.Value.Where(t => GetOrderInFile(t) != 0).FirstOrDefault() != null).ForEach(t => t.Value.Sort((x, y) => GetOrderInFile(x) - GetOrderInFile(y))); var byFile2 = new Dictionary <JsFile, List <ITypeDefinition> >(); foreach (var pair in byFile) { var file = new JsFile { Filename = pair.Key, Units = new List <JsUnit> { new JsUnit { Statements = new List <JsStatement>() } } }; byFile2.Add(file, pair.Value); } if (BeforeExportTypes != null) { BeforeExportTypes(byFile2); } //export by filenames and order byFile2.ForEachParallel(ExportTypesInFile); JsFiles = byFile2.Keys.ToList(); if (Sk.ExportTsHeaders(Compiler.Project.Compilation)) { ExportTsHeaders(allTypesToExport); } }
JsFile CreateExternalJsFile(string filename) { var unit = new JsUnit { Statements = new List <JsStatement>() }; var st = new JsCodeStatement { Code = File.ReadAllText(filename) }; var file = new JsFile { Filename = filename, Units = new List <JsUnit> { unit } }; unit.Statements.Add(st); return(file); }
public List <ChangeDescriptor> BuildChangeList(Endpoint endpoint, JsFile jsFile, int fromVersion, int toVersion) { var changes = new List <ChangeDescriptor>(); if (!_entries.ContainsKey(endpoint.Pedigree)) { return(null); } var epEntry = _entries[endpoint.Pedigree]; for (var i = fromVersion + 1; i <= toVersion; i++) { var jsFileKey = $"{jsFile.Filename.ToLower()}.{i}"; if (!epEntry.ContainsKey(jsFileKey) || epEntry[jsFileKey].Count == 0) { continue; } var applicableChanges = epEntry[jsFileKey].Where(chg => { // only add if unique (as same sproc could have be changed across versions) if (changes.Exists(existing => existing.Description.Equals(chg.Description, StringComparison.OrdinalIgnoreCase))) { return(false); } else { return(true); } }); changes.AddRange(applicableChanges); } return(changes); }
private IActionResult ServeTypescriptDefinition(Endpoint endpoint, JsFile jsFile) { // if (jsFile == null && app != null) // { // // TODO: Get server ip/dns name??? // var refs = app.JsFiles.Select(f => $"/// <reference path=\"./api/tsd/{f.Id}\" />").ToArray(); // string content = ""; // if (refs.Length > 0) // { // content = string.Join("\r\n", refs); // } // return Ok(content); // } var tsdFilePath = endpoint.OutputTypeScriptTypingsFilePath(jsFile); if (!System.IO.File.Exists(tsdFilePath)) { return(NotFound()); } byte[] tsdData; using (var fs = System.IO.File.Open(tsdFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { tsdData = new byte[fs.Length]; fs.Read(tsdData, 0, tsdData.Length); } Response.Headers.Add("jsfver", jsFile.Version.ToString()); return(Content(System.Text.Encoding.UTF8.GetString(tsdData))); }
public ApiResponse GetAllRoutines([FromQuery] string project, [FromQuery(Name = "app")] string appName, [FromQuery] string file) { try { if (!ControllerHelper.GetProjectAndApp(project, appName, out var proj, out var app, out var resp)) { return(resp); } if (!app.GetUnifiedCacheListWithApiResponse(out var allRoutines, out resp)) { return(resp); } JsFile jsFile = null; if (file != null) { jsFile = app.GetJsFile(file); if (jsFile == null) { return(ApiResponse.ExclamationModal("The specified output file was not found.")); } } if (jsFile == null) { var ret = allRoutines.Where(row => !row.IsDeleted) .OrderBy(a => a.FullName) .Select(routine => { var instruction = jsdal_server_core.Settings .ObjectModel .RoutineIncludeExcludeInstruction.Create(routine, app.Rules, (DefaultRuleMode)app.DefaultRuleMode); return(new { RoutineFullName = routine.FullName, Included = instruction.Included ?? false, Excluded = instruction.Excluded ?? false, Reason = instruction.Reason, Source = instruction.Source }); }); return(ApiResponse.Payload(new { Routines = ret, app.DefaultRuleMode })); } else { var ret = allRoutines.Where(row => !row.IsDeleted) .OrderBy(a => a.FullName) .Select(routine => { var instruction = jsdal_server_core.Settings .ObjectModel .RoutineIncludeExcludeInstruction.Create(routine, app.Rules, (DefaultRuleMode)app.DefaultRuleMode, jsFile.Rules); return(new { RoutineFullName = routine.FullName, Included = instruction.Included ?? false, Excluded = instruction.Excluded ?? false, Reason = instruction.Reason, Source = instruction.Source }); }); return(ApiResponse.Payload(new { Routines = ret, app.DefaultRuleMode })); } } catch (Exception ex) { return(ApiResponse.Exception(ex)); } }
public static void GenerateJsFileV2(string source, Endpoint endpoint, JsFile jsFile, Dictionary <string, ChangeDescriptor> fullChangeSet = null, bool rulesChanged = false) { var generateMetric = new Performance.ExecutionBase("GenerateJsFileV2"); var noChanges = false; try { // TODO: Figure out out casing on this property string jsNamespace = null;//endpoint.JsNamespace; if (string.IsNullOrWhiteSpace(jsNamespace)) { jsNamespace = endpoint.MetadataConnection.InitialCatalog; } var jsSafeNamespace = MakeNameJsSafe(jsNamespace); var routineContainerTemplate = WorkSpawner.TEMPLATE_RoutineContainer; var routineTemplate = WorkSpawner.TEMPLATE_Routine; var typescriptDefinitionsContainer = WorkSpawner.TEMPLATE_TypescriptDefinitions; endpoint.ApplyRules(jsFile); var includedRoutines = (from row in endpoint.CachedRoutines where !row.IsDeleted && (row.RuleInstructions[jsFile]?.Included ?? false == true) orderby row.FullName select row).ToList(); List <KeyValuePair <string, ChangeDescriptor> > changesInFile = null; if (fullChangeSet != null) { changesInFile = fullChangeSet.Where(change => includedRoutines.Count(inc => inc.FullName.Equals(change.Key, StringComparison.OrdinalIgnoreCase)) > 0).ToList(); // TODO: Consider if this is a good idea? // If we can reasonably say that there are no changes to routines that this JsFile cares about, why regenerate this file and why give it a new Version if (changesInFile.Count == 0) { noChanges = true; return; } } var jsSchemaLookupForJsFunctions = new Dictionary <string, List <string> /*Routine defs*/>(); var tsSchemaLookup = new Dictionary <string, List <string> /*Routine defs*/>(); var typeScriptParameterAndResultTypesSB = new StringBuilder(); var serverMethodPlugins = PluginLoader.Instance.PluginAssemblies .SelectMany(pa => pa.Plugins) .Where(p => p.Type == PluginType.ServerMethod && endpoint.Application.IsPluginIncluded(p.Guid.ToString())); var uniqueSchemas = new List <string>(); var mainLoopMetric = generateMetric.BeginChildStage("Main loop"); includedRoutines.ForEach(r => { try { if (r.TypescriptMethodStub == null) { r.PrecalculateJsGenerationValues(endpoint); } var jsSchemaName = JsFileGenerator.MakeNameJsSafe(r.Schema); var jsFunctionName = JsFileGenerator.MakeNameJsSafe(r.Routine); if (!jsSchemaLookupForJsFunctions.ContainsKey(jsSchemaName)) { jsSchemaLookupForJsFunctions.Add(jsSchemaName, new List <string>()); } if (!tsSchemaLookup.ContainsKey(jsSchemaName)) { tsSchemaLookup.Add(jsSchemaName, new List <string>()); } if (!uniqueSchemas.Contains(r.Schema)) { uniqueSchemas.Add(r.Schema); } var schemaIx = uniqueSchemas.IndexOf(r.Schema); // .js { var jsFunctionDefLine = routineTemplate.Replace("<<FUNC_NAME>>", jsFunctionName).Replace("<<SCHEMA_IX>>", schemaIx.ToString()).Replace("<<ROUTINE>>", r.Routine); if (r.Type.Equals(string.Intern("PROCEDURE"), StringComparison.OrdinalIgnoreCase)) { jsFunctionDefLine = jsFunctionDefLine.Replace("<<CLASS>>", "S"); } else { jsFunctionDefLine = jsFunctionDefLine.Replace("<<CLASS>>", "U"); } jsSchemaLookupForJsFunctions[jsSchemaName].Add(jsFunctionDefLine); } // .tsd { typeScriptParameterAndResultTypesSB.AppendLine(r.TypescriptParameterTypeDefinition); typeScriptParameterAndResultTypesSB.AppendLine(r.TypescriptOutputParameterTypeDefinition); typeScriptParameterAndResultTypesSB.AppendLine(r.TypescriptResultSetDefinitions); tsSchemaLookup[jsSchemaName].Add(r.TypescriptMethodStub); } } catch (Exception ex) { SessionLog.Exception(ex); // TODO: quit whole process } }); mainLoopMetric.End(); var finalSBMetric = generateMetric.BeginChildStage("Final SB"); var schemaAndRoutineDefs = string.Join("\r\n", jsSchemaLookupForJsFunctions.Select(s => "\tx." + s.Key + " = {\r\n\t\t" + string.Join(",\r\n\t\t", s.Value.ToArray()) + "\r\n\t}\r\n").ToArray()); var tsSchemaAndRoutineDefs = string.Join("\r\n", tsSchemaLookup.Select(s => "\t\tclass " + s.Key + " {\r\n" + string.Join(";\r\n", s.Value.ToArray()) + "\r\n\t\t}\r\n").ToArray()); var finalSB = new StringBuilder(routineContainerTemplate); jsFile.IncrementVersion(); // record changes against new version if (changesInFile != null && changesInFile.Count > 0) { JsFileChangesTracker.Instance.AddUpdate(endpoint, jsFile, changesInFile.Select(kv => kv.Value).ToList()); } if (rulesChanged) { JsFileChangesTracker.Instance.AddUpdate(endpoint, jsFile, new List <ChangeDescriptor> { ChangeDescriptor.Create("System", "One or more rules changed.") }); } finalSB.Replace("<<DATE>>", DateTime.Now.ToString("dd MMM yyyy, HH:mm")) .Replace("<<FILE_VERSION>>", jsFile.Version.ToString()) .Replace("<<SERVER_NAME>>", Environment.MachineName) .Replace("<<ENDPOINT>>", endpoint.Pedigree) .Replace("<<UNIQUE_SCHEMAS>>", string.Join(',', uniqueSchemas.Select(k => $"'{k}'"))) .Replace("<<Catalog>>", jsSafeNamespace) .Replace("<<ROUTINES>>", schemaAndRoutineDefs) ; var finalTypeScriptSB = new StringBuilder(); finalTypeScriptSB = finalTypeScriptSB.Append(typescriptDefinitionsContainer); // Custom/User types if (endpoint.CustomTypeLookupWithTypeScriptDef.Count > 0) { var customTSD = from kv in endpoint.CustomTypeLookupWithTypeScriptDef select $"\t\ttype {kv.Key} = {kv.Value};"; typeScriptParameterAndResultTypesSB.Insert(0, string.Join("\r\n", customTSD)); } var resultAndParameterTypes = typeScriptParameterAndResultTypesSB.ToString(); finalTypeScriptSB.Replace("<<DATE>>", DateTime.Now.ToString("dd MMM yyyy, HH:mm")) .Replace("<<FILE_VERSION>>", jsFile.Version.ToString()) .Replace("<<SERVER_NAME>>", Environment.MachineName) .Replace("<<Catalog>>", jsSafeNamespace) .Replace("<<ResultAndParameterTypes>>", resultAndParameterTypes) .Replace("<<MethodsStubs>>", tsSchemaAndRoutineDefs) ; finalSBMetric.End(); var toStringMetric = generateMetric.BeginChildStage("ToString"); var typescriptDefinitionsOutput = finalTypeScriptSB.ToString(); var finalOutput = finalSB.ToString(); toStringMetric.End(); var filePath = endpoint.OutputFilePath(jsFile); var minfiedFilePath = endpoint.MinifiedOutputFilePath(jsFile); var tsTypingsFilePath = endpoint.OutputTypeScriptTypingsFilePath(jsFile); var minifyMetric = generateMetric.BeginChildStage("Minify"); var minifiedSource = Uglify.Js(finalOutput /*, { }*/).Code; minifyMetric.End(); if (!Directory.Exists(endpoint.OutputDir)) { Directory.CreateDirectory(endpoint.OutputDir); } var fileOutputMetric = generateMetric.BeginChildStage("Write"); var jsFinalBytes = System.Text.Encoding.UTF8.GetBytes(finalOutput); var jsFinalMinifiedBytes = System.Text.Encoding.UTF8.GetBytes(minifiedSource); jsFile.ETag = Controllers.PublicController.ComputeETag(jsFinalBytes); jsFile.ETagMinified = Controllers.PublicController.ComputeETag(jsFinalMinifiedBytes); File.WriteAllText(filePath, finalOutput); File.WriteAllText(minfiedFilePath, minifiedSource); File.WriteAllText(tsTypingsFilePath, typescriptDefinitionsOutput); fileOutputMetric.End(); } finally { generateMetric.End(); SessionLog.InfoToFileOnly($"{endpoint.Pedigree.PadRight(25, ' ')} - {generateMetric.DurationInMS.ToString().PadLeft(4)} ms {jsFile.Filename.PadRight(20)} (source={source};rulesChanged={rulesChanged};changes={!noChanges}); {generateMetric.ChildDurationsSingleLine()}"); } }
void ConvertTypeDefinition(ITypeDefinition ce, JsFile jsFile) { var unit = ConvertTypeDefinition(ce); jsFile.Units[0].Statements.AddRange(unit.Statements); }