public override bool Execute(List <string> args) { if (args.Count != 2) { return(false); } var outDir = args[1]; ITagLayoutWriter writer; switch (args[0]) { case "csharp": writer = new CSharpClassWriter(_stringIds, outDir); break; case "cpp": writer = new CppStructWriter(_stringIds, outDir); break; default: return(false); } Directory.CreateDirectory(outDir); var count = 0; using (var stream = _fileInfo.OpenRead()) { foreach (var tagClass in _cache.TagClasses) { TagLayoutGuess layout = null; HaloTag lastTag = null; foreach (var tag in _cache.Tags.FindAllByClass(tagClass)) { Console.Write("Analyzing "); TagPrinter.PrintTagShort(tag); lastTag = tag; var analyzer = new TagAnalyzer(_cache, tag); var data = _cache.ExtractTag(stream, tag); var tagLayout = analyzer.Analyze(data); if (layout != null) { layout.Merge(tagLayout); } else { layout = tagLayout; } } if (layout != null && lastTag != null) { Console.WriteLine("Writing {0} layout", tagClass); LayoutGuessWriter.Write(lastTag, layout, writer); count++; } } } Console.WriteLine("Successfully generated {0} layouts!", count); return(true); }
/// <summary> /// This method builds the classes for the current project. /// If Write Classes is false the database schema will be /// loaded the classes will not be built. /// </summary> /// <param name="currentProject"></param> /// <param name="fileManager"></param> /// <param name="writeClasses"></param> /// <returns></returns> public bool BuildClasses(Project currentProject, ProjectFileManager fileManager, bool writeClasses) { // initial value bool success = false; // Create DataManager this.Setup(currentProject); // 12.19.2021 TargetFrameworkEnum targetFramework = (TargetFrameworkEnum)currentProject.TargetFramework; // Create New ClassWriter Object CSharpClassWriter classWriter = new CSharpClassWriter(fileManager, BusinessObjectPass, false, targetFramework); // Add Databases AddDatabases(ref currentProject); // if write classes if (writeClasses) { // Write DataManager success = classWriter.WriteDataClasses(DataManager); } else { // set success to true success = true; } // return value return(success); }
private void btnRefresTables_Click(object sender, EventArgs e) { lstTables.Items.Clear(); mapper = new ORMapper(); Database database = DatabaseFactory.CreateDatabase(); tableReader = new DbTableReader(database); classWriter = new CSharpClassWriter(); classLogicWriter = new CSharpClassLogicWriter(); mapWriter = new XmlMapWriter(); queryParaWriter = new CSharpClassQueryParaWriter(); lstTables.Items.AddRange(Misc.GetDbTables(database)); }
/// <summary> /// This method returns the Parameters Text /// </summary> public static string GetParametersText(ref FieldSet fieldSet, List <Enumeration> enumerations) { // initial value string parametersText = ""; // locals int count = 0; string parameterFieldDataType = ""; string fieldParameter = ""; // If the fieldSet object exists if (NullHelper.Exists(fieldSet)) { // load the fields for this fieldSet fieldSet.Fields = FieldSetHelper.LoadFieldSetFields(fieldSet.FieldSetId); // If the fields collection exists and has one or more items if (ListHelper.HasOneOrMoreItems(fieldSet.Fields)) { // Create a new instance of a 'StringBuilder' object. StringBuilder sb = new StringBuilder(); // Iterate the collection of DTNField objects foreach (DTNField field in fieldSet.Fields) { // Increment the value for count count++; // convert the DTN Field back to a DataField DataField parameterField = DataConverter.ConvertDataField(field); // If the value for the property field.IsEnumeration is true if (field.DataType.ToString().ToLower() == "enumeration") { // set the dataType and fieldName parameterFieldDataType = EnumerationHelper.GetEnumerationDataType(field.FieldName, enumerations); } else { // Convert the DataType parameterFieldDataType = CSharpClassWriter.ConvertDataType(parameterField); } // set the dataType and fieldName fieldParameter = parameterFieldDataType + " " + CSharpClassWriter.CapitalizeFirstCharEx(field.FieldName, true); // if this is after the firstItem if (count > 1) { // append a comma and a space sb.Append(", "); } // append the paremter sb.Append(fieldParameter); } // set the return value parametersText = sb.ToString(); } } // return value return(parametersText); }
/// <summary> /// event is fired when Create Blazor Services Button _ Link Clicked /// </summary> private void CreateBlazorServicesButton_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { // locals int attempts = 0; string path = ""; string path2 = ""; bool filesCreated = false; string message = ""; try { // if the value for HasServicesFolder is true if ((HasServicesFolder) && (HasTable)) { // Setup the Graph Graph.Maximum = 20; Graph.Minimum = 0; Graph.Value = 0; // Show the graph Graph.Visible = true; // Create a Process to launch a command window (hidden) to create the item templates Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.FileName = "cmd.exe"; startInfo.WorkingDirectory = Project.ServicesFolder; startInfo.Arguments = "/C " + CreateServices; process.StartInfo = startInfo; process.Start(); // next I am performing a wait for the files to be here, which on different people's machines // speeds I am allowing up to 5 seconds. My machine takes about half a second maybe more, // so 5 should be enough or give up. If it doesn't work for and you haven't upgraded your // computer in a while, maybe you should buy a new computer. // If your internet is slow and this takes longer than 5 seconds let me know and I will extend // the wait. do { // Increment the value for attempt attempts++; // Show the graph Graph.Value = attempts; // refresh everything this.Refresh(); // Try every half second System.Threading.Thread.Sleep(500); // get the path path = Path.Combine(Project.ServicesFolder, DataWatcherFileName); // if the file exists if (File.Exists(path)) { // The files were created filesCreated = true; // break out of the loop break; } }while (attempts < 20); // Wait one extra half second after the file is avialable before trying to modify it System.Threading.Thread.Sleep(500); // if the files were created if (filesCreated) { // *************************************** // ************** DataWatcher.cs Class ************* // *************************************** // Now the file must be read string fileText = File.ReadAllText(path); // now create the replaceParameter values string tableName = table.TableName; string variableName = CSharpClassWriter.CapitalizeFirstCharEx(table.TableName, true); string pluralTableName = PluralWordHelper.GetPluralName(table.TableName, false); string pluralVariableName = PluralWordHelper.GetPluralName(table.TableName, true); string primaryKeyDataType = ""; string primaryKeyVariableName = ""; string primaryKeyPropertyName = ""; // Update 12.12.2020: The DataService (for Blazor) requires the PrimaryKey // cataType and field name. DTNField field = FindPrimaryKey(table); // if the field was found if (NullHelper.Exists(field)) { // get the dataType primaryKeyDataType = field.DataType.ToString().ToLower(); primaryKeyVariableName = CSharpClassWriter.CapitalizeFirstCharEx(field.FieldName, true); primaryKeyPropertyName = CSharpClassWriter.CapitalizeFirstCharEx(field.FieldName, false); // if an autonumber identity field (which most will be) if (primaryKeyDataType == "autonumber") { // switch to int primaryKeyDataType = "int"; } } // string parameterDataType = FindPrimaryKey // Replace out the fileText replacement parameters fileText = fileText.Replace("[TableName]", tableName); fileText = fileText.Replace("[VariableName]", variableName); fileText = fileText.Replace("[PluralVariableName]", pluralVariableName); // Delete the current file at path File.Delete(path); // rename the file path = path.Replace("DataWatcher.cs", Table.TableName + "DataWatcher.cs"); // Write out the next text File.WriteAllText(path, fileText); // *************************************** // ************** Service.cs Class ************* // *************************************** // now change path to the Service class path2 = Path.Combine(Project.ServicesFolder, ServiceFileName); // Now the file must be read fileText = File.ReadAllText(path2); // Replace out the fileText replacement parameters fileText = fileText.Replace("[TableName]", tableName); fileText = fileText.Replace("[VariableName]", variableName); fileText = fileText.Replace("[PluralVariableName]", pluralVariableName); fileText = fileText.Replace("[PluralTableName]", pluralTableName); fileText = fileText.Replace("[ParameterDataType]", primaryKeyDataType); fileText = fileText.Replace("[PrimaryKey]", primaryKeyVariableName); fileText = fileText.Replace("[PrimaryKeyPropertyName]", primaryKeyPropertyName); // Delete the current file at path2 File.Delete(path2); // rename the file path2 = path2.Replace("Service.cs", Table.TableName + "Service.cs"); // Write out the next text File.WriteAllText(path2, fileText); // Show a message message = "The following classes were created:" + Environment.NewLine + path + Environment.NewLine + path2; // Hide the graph Graph.Visible = false; // Show the user a message MessageBoxHelper.ShowMessage(message, "Files Created"); } else { // change the message message = "Oops, something went wrong. Step through the code in DataTier.Net.Client.Controls.BlazorDataServices.cs, event name CreateBlazorServicesButton_Click."; // Show the user a message MessageBoxHelper.ShowMessage(message, "Files Could Not Be Created", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } catch (Exception error) { // Set the error DebugHelper.WriteDebugError("CreateBlazorServicesButton_LinkClicked", this.Name, error); // show the user a message MessageBoxHelper.ShowMessage("The Blazor Data Services Either Were Not Installed Or You Do Not Have Permission To Create Files In This Directory", "Create Data Services Failed"); } }
/// <summary> /// Write the Classes as C# Code /// </summary> /// <param name="inOutPath"></param> /// <param name="tmpClassList"></param> /// <param name="inProjectInformation"></param> /// <param name="tmpReplacer"></param> private static void WriteCSharpCode(string inOutPath, ProjectInformation inProjectInformation, List <StringReplacement> inReplacementList) { var tmpWriter = new CSharpClassWriter(); //Predo StringReplacements var tmpList = new List <Tuple <Regex, StringReplacement> >(); foreach (var tmpEntry in inReplacementList) { tmpList.Add(new Tuple <Regex, StringReplacement> ( new Regex(tmpEntry.NameRegex, RegexOptions.Compiled) , tmpEntry )); } foreach (var tmpClass in inProjectInformation.ClassList) { if (tmpClass.ClassType == ClassTypeEnum.System) { continue; } NamespaceHelper.HandleNamespaces(tmpClass); var tmpCSharp = tmpWriter.CreateClassFile(tmpClass).Content; foreach (var tmpReplacement in tmpList) { if (tmpReplacement.Item1.IsMatch(tmpClass.Name)) { if (!string.IsNullOrEmpty(tmpReplacement.Item2.RequiredUsing)) { if (tmpCSharp.Contains(tmpReplacement.Item2.SourceText) && !tmpCSharp.Contains(tmpReplacement.Item2.RequiredUsing)) { tmpCSharp = $"using {tmpReplacement.Item2.RequiredUsing};{Environment.NewLine}{tmpCSharp}"; } } tmpCSharp = tmpCSharp.Replace(tmpReplacement.Item2.SourceText, tmpReplacement.Item2.ReplacementText); } } var tmpNewNamespace = tmpClass.Namespace.Split('.'); var tmpNewPath = Path.Combine(inOutPath, Path.Combine(tmpNewNamespace)); Directory.CreateDirectory(tmpNewPath); File.WriteAllText(Path.Combine(tmpNewPath, tmpClass.Name + ".cs"), tmpCSharp); } //Write UnknownType File var tmpUnknownFilePaths = new List <string>(); var tmpUsings = new List <string>(); foreach (var tmpClass in inProjectInformation.GetAllUnknownTypes()) { var tmpCSharp = new StringBuilder(); tmpClass.ModifierList.Add("public"); tmpClass.ModifierList.Add("class"); new CSharpClassWriter().AddClassContainerString(tmpClass, tmpCSharp, 1); tmpUnknownFilePaths.Add(tmpCSharp.ToString()); } //Write unknown Types into File var tmpUnknownFile = $@" {string.Join(Environment.NewLine, tmpUsings.OrderBy(inItem => inItem).Select(inItem => $"using {inItem};"))} namespace UnknownTypes {{ {string.Join(Environment.NewLine + Environment.NewLine, tmpUnknownFilePaths)} }}"; Directory.CreateDirectory(Path.Combine(inOutPath)); File.WriteAllText(Path.Combine(inOutPath, "UnknownTypes.cs"), tmpUnknownFile); }