示例#1
0
        private void GenerateCss()
        {
            string relativeFile = Path.Combine(@"Content\site\", "site.less");
            string file         = Path.Combine(siteWebPath, relativeFile);

            Directory.CreateDirectory(Path.GetDirectoryName(file));
            if (!File.Exists(file))
            {
                CodeFileHelper.CheckoutAndWrite(file, "\r\n", false);
            }

            string code = Templates.Render(new Views.EntityCss(), model);

            using (var ms = new MemoryStream())
            {
                var old = File.ReadAllBytes(file);
                if (old.Length > 0)
                {
                    ms.Write(old, 0, old.Length);
                }
                using (var sw = new StreamWriter(ms, utf8))
                {
                    AppendComment(sw);
                    sw.Write(code);
                    sw.Flush();

                    CodeFileHelper.CheckoutAndWrite(file, ms.ToArray(), false);
                }
            }

            ProjectFileHelper.AddFileToProject(siteWebProj, relativeFile);
        }
示例#2
0
        public EntityCodeGenerator(EntityCodeGenerationModel model, GeneratorConfig config)
        {
            var kdiff3Paths = new[]
            {
                config.KDiff3Path,
                Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "KDiff3\\kdiff3.exe"),
                Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "KDiff3\\kdiff3.exe"),
            };

            this.model = model;
            CodeFileHelper.Kdiff3Path = kdiff3Paths.FirstOrDefault(File.Exists);

            if (config.TFSIntegration)
            {
                CodeFileHelper.SetupTFSIntegration(config.TFPath);
            }

            CodeFileHelper.SetupTSCPath(config.TSCPath);

            siteWebProj = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, config.WebProjectFile));
            siteWebPath = Path.GetDirectoryName(siteWebProj);
            if (!string.IsNullOrEmpty(config.ScriptProjectFile))
            {
                scriptProject = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, config.ScriptProjectFile));
                scriptPath    = Path.GetDirectoryName(scriptProject);

                if (!File.Exists(scriptProject))
                {
                    scriptProject = null;
                    scriptPath    = null;
                }
            }

            this.config = config;
        }
示例#3
0
        private static void CreateFile(string code, string file)
        {
            var backup = CreateDirectoryOrBackupFile(file);

            CodeFileHelper.CheckoutAndWrite(file, code, true);
            CodeFileHelper.MergeChanges(backup, file);
        }
示例#4
0
        public static void MergeChanges(string backup, string file)
        {
            if (backup == null || !File.Exists(backup) || !File.Exists(file))
            {
                return;
            }

            bool isEqual;

            using (var fs1 = new FileStream(backup, FileMode.Open))
                using (var fs2 = new FileStream(file, FileMode.Open))
                    isEqual = CodeFileHelper.StreamsContentsAreEqual(fs1, fs2);

            if (isEqual)
            {
                File.Delete(backup);
                return;
            }

            var generated = Path.ChangeExtension(file, Path.GetExtension(file) + ".gen.bak");

            File.Copy(file, generated, true);
            File.Copy(backup, file, true);

            if (Kdiff3Path.IsNullOrEmpty() ||
                !File.Exists(Kdiff3Path))
            {
                throw new InvalidOperationException(String.Format("KDiff3, verilen '{0}' konumunda bulunamadı!",
                                                                  Kdiff3Path ?? ""));
            }

            Process.Start(Kdiff3Path, "--auto " + file + " " + generated + " -o " + file);
        }
示例#5
0
        private void CreateFile(string code, string file, string dependentUpon = null)
        {
            var backup = CreateDirectoryOrBackupFile(file);

            CodeFileHelper.CheckoutAndWrite(file, code, true);
            CodeFileHelper.MergeChanges(backup, file);
        }
示例#6
0
        private void CreateNewSiteWebFile(string code, string relativeFile, string dependentUpon = null)
        {
            string file   = Path.Combine(rootDir, relativeFile);
            var    backup = CreateDirectoryOrBackupFile(file);

            CodeFileHelper.CheckoutAndWrite(file, code, true);
            CodeFileHelper.MergeChanges(backup, file);
        }
示例#7
0
        private void CreateNewSiteScriptFile(string code, string relativeFile, string dependentUpon = null)
        {
            string file   = Path.Combine(scriptPath, relativeFile);
            var    backup = CreateDirectoryOrBackupFile(file);

            CodeFileHelper.CheckoutAndWrite(file, code, true);
            CodeFileHelper.MergeChanges(backup, file);
            ProjectFileHelper.AddFileToProject(scriptProject, relativeFile, dependentUpon);
        }
示例#8
0
        private void CreateNewSiteScriptFile(string code, string relativeFile, string dependentUpon = null)
        {
            string file   = Path.Combine(scriptPath, relativeFile);
            var    backup = CreateDirectoryOrBackupFile(file);

            using (var sw = new StreamWriter(file, false, utf8))
                sw.Write(code);
            CodeFileHelper.MergeChanges(backup, file);
            ProjectFileHelper.AddFileToProject(scriptProject, relativeFile, dependentUpon);
        }
示例#9
0
        private void CreateNewSiteWebFile(string code, string relativeFile)
        {
            string file   = Path.Combine(siteWebPath, relativeFile);
            var    backup = CreateDirectoryOrBackupFile(file);

            using (var sw = new StreamWriter(file, false, utf8))
                sw.Write(code);
            CodeFileHelper.MergeChanges(backup, file);
            ProjectFileHelper.AddFileToProject(siteWebProj, relativeFile);
        }
示例#10
0
        private void CreateFile(string code, string file, string dependentUpon = null)
        {
            var backup = CreateDirectoryOrBackupFile(file);

            CodeFileHelper.CheckoutAndWrite(file, code, true);
            CodeFileHelper.MergeChanges(backup, file);
#if ASPNETMVC
            ProjectFileHelper.AddFileToProject(this.csproj,
                                               file.Substring(Path.GetDirectoryName(csproj).Length + 1).Replace('/', '\\'), dependentUpon);
#endif
        }
示例#11
0
        private string BackupFile(string file)
        {
            if (File.Exists(file))
            {
                var backupFile = string.Format("{0}.{1}.bak", file, DateTime.Now.ToString("yyyyMMdd_HHmmss"));
                CodeFileHelper.CheckoutAndWrite(backupFile, File.ReadAllBytes(file), false);
                return(backupFile);
            }

            return(null);
        }
示例#12
0
 private string CreateDirectoryOrBackupFile(string file)
 {
     if (File.Exists(file))
     {
         var backupFile = string.Format("{0}.{1}.bak", file, DateTime.Now.ToString("yyyyMMdd_HHmmss"));
         CodeFileHelper.CheckoutAndWrite(backupFile, File.ReadAllBytes(file), false);
         return(backupFile);
     }
     else
     {
         Directory.CreateDirectory(Path.GetDirectoryName(file));
         return(null);
     }
 }
示例#13
0
        public void Run()
        {
            if (!scriptPath.IsEmptyOrNull())
            {
                Directory.CreateDirectory(scriptPath);
            }

            Directory.CreateDirectory(siteWebPath);

            GenerateRow();
            GenerateCss();
            GenerateColumns();
            GenerateForm();
            GenerateRepository();
            GenerateEndpoint();
            GeneratePageController();
            GeneratePageIndex();

            if (config.GenerateSSImports && scriptProject != null)
            {
                GenerateScriptRowSS();
                GenerateScriptServiceSS();
                GenerateScriptFormSS();
            }

            if (config.GenerateTSTypings)
            {
                GenerateScriptRowTS();
                GenerateScriptServiceTS();
                GenerateScriptFormTS();
            }

            if (config.GenerateTSCode)
            {
                GenerateScriptGridTS();
                GenerateScriptDialogTS();
            }
            else if (scriptProject != null)
            {
                GenerateScriptGridSS();
                GenerateScriptDialogSS();
            }

            if (config.GenerateTSCode ||
                config.GenerateTSTypings)
            {
                CodeFileHelper.ExecuteTSC(Path.Combine(siteWebPath, @"Scripts\"), "");
            }
        }
示例#14
0
        private void GenerateNavigationLink()
        {
            string file = Path.Combine(rootDir, string.IsNullOrEmpty(model.Module) ?
                                       "Modules/Common/Navigation/NavigationItems.cs" :
                                       "Modules/" + model.ModuleSlash + model.Module + "Navigation.cs");

            file = file.Replace('/', Path.DirectorySeparatorChar);

            string code = Templates.Render("NavigationLink", model);

            if (!File.Exists(file))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(file));
                CreateFile(code, file);
            }
            else
            {
                var lines      = File.ReadAllLines(file).ToList();
                var toInsert   = code.Replace("\r", "", StringComparison.Ordinal).Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                var usingIndex = lines.FindLastIndex(x => x.TrimToEmpty().StartsWith("using ", StringComparison.Ordinal));
                if (usingIndex < 0)
                {
                    usingIndex = 0;
                }

                foreach (var usng in toInsert.Where(x => x.TrimToEmpty().StartsWith("using ", StringComparison.Ordinal)))
                {
                    if (lines.Find(x => x.TrimToEmpty().Replace(" ", "", StringComparison.Ordinal)
                                   .IsTrimmedSame(usng.TrimToEmpty().Replace(" ", "", StringComparison.Ordinal))) == null)
                    {
                        lines.Insert(usingIndex, usng);
                        usingIndex++;
                    }
                }

                if (!lines.Any(x => x.Contains("MyPages." + model.ClassName + "Controller", StringComparison.Ordinal)))
                {
                    var insertIndex = lines.FindLastIndex(x => !string.IsNullOrWhiteSpace(x)) + 1;
                    foreach (var z in toInsert.Where(x => !string.IsNullOrWhiteSpace(x) &&
                                                     !x.TrimToEmpty().StartsWith("using ", StringComparison.Ordinal)))
                    {
                        lines.Insert(insertIndex, z);
                    }
                }

                CodeFileHelper.CheckoutAndWrite(file, string.Join(Environment.NewLine, lines), false);
            }
        }
示例#15
0
        private void CreateFile(string code, string file, string dependentUpon = null)
        {
            var backup = CreateDirectoryOrBackupFile(file);

            CodeFileHelper.CheckoutAndWrite(file, code, true);
            CodeFileHelper.MergeChanges(backup, file);

            //ROLEMBERG FILHO - GERA EM ASPNETCORE
            if (!config.AspNetCore)
            {
                //#if !ASPNETCORE
                ProjectFileHelper.AddFileToProject(this.csproj,
                                                   file.Substring(Path.GetDirectoryName(csproj).Length + 1).Replace('/', '\\'), dependentUpon);
                //#endif
            }
            //ROLEMBERG FILHO - GERA EM ASPNETCORE
        }
        public static void MergeChanges(string backup, string file)
        {
            if (backup == null || !File.Exists(backup) || !File.Exists(file))
            {
                return;
            }

            bool isEqual;

            using (var fs1 = new FileStream(backup, FileMode.Open))
                using (var fs2 = new FileStream(file, FileMode.Open))
                    isEqual = CodeFileHelper.StreamsContentsAreEqual(fs1, fs2);

            if (isEqual)
            {
                File.Delete(backup);
                return;
            }

            var generated = Path.ChangeExtension(file, Path.GetExtension(file) + ".gen.bak");

            CheckoutAndWrite(generated, File.ReadAllBytes(file), false);
            CheckoutAndWrite(file, File.ReadAllBytes(backup), true);

            if (Kdiff3Path.IsNullOrEmpty() ||
                !File.Exists(Kdiff3Path))
            {
                if (Kdiff3Path.IsNullOrEmpty())
                {
                    throw new Exception(
                              "Couldn't locate KDiff3 utility which is required to merge changes. " +
                              "Please install it, or if it is not installed to default location, " +
                              "set its path in CodeGenerator.config file!");
                }

                throw new Exception(String.Format(
                                        "Couldn't locate KDiff3 utility at '{0}' which is required to merge changes. " +
                                        "Please install it, or if it is not installed to default location, " +
                                        "set its path in CodeGenerator.config file!", Kdiff3Path));
            }

            Process.Start(Kdiff3Path, "--auto \"" + file + "\" \"" + generated + "\" -o \"" + file + "\"");
        }
示例#17
0
        private void GenerateCss()
        {
            string relativeFile = Path.Combine(@"Content/site/".Replace('/', Path.DirectorySeparatorChar), "site" +
                                               (!string.IsNullOrEmpty(model.Module) ? ("." + model.Module.ToLowerInvariant()) : "") + ".less");

            string file = Path.Combine(rootDir, relativeFile);

            Directory.CreateDirectory(Path.GetDirectoryName(file));
            if (!File.Exists(file))
            {
                if (!string.IsNullOrEmpty(model.Module))
                {
                    relativeFile = Path.Combine("Content/site/".Replace('/', Path.DirectorySeparatorChar), "site.less");
                    file         = Path.Combine(rootDir, relativeFile);
                }

                if (!File.Exists(file))
                {
                    CodeFileHelper.CheckoutAndWrite(file, Environment.NewLine, false);
                }
            }

            string code = Templates.Render(new Views.EntityCss(), model);

            using (var ms = new MemoryStream())
            {
                var old = File.ReadAllBytes(file);
                if (old.Length > 0)
                {
                    ms.Write(old, 0, old.Length);
                }
                using (var sw = new StreamWriter(ms, utf8))
                {
                    sw.Write(code);
                    sw.Flush();

                    CodeFileHelper.CheckoutAndWrite(file, ms.ToArray(), false);
                }
            }
        }
示例#18
0
        private void GenerateCodes_Click(object sender, RoutedEventArgs e)
        {
            var conn = (GeneratorConfig.Connection) this.ConnectionsCombo.SelectedItem;

            if (conn == null)
            {
                MessageBox.Show("A connection must be selected!");
                return;
            }

            var tables = this._tables.Where(x => x.IsChecked == true);

            if (this.ConnectionsCombo.SelectedItem == null)
            {
                MessageBox.Show("Please select at least one table!");
                return;
            }
            ;

            var noIdentifier = tables.FirstOrDefault(x => x.Identifier.IsTrimmedEmpty());

            if (noIdentifier != null)
            {
                MessageBox.Show("Identifier for table " + noIdentifier.FullName + " is empty!");
                return;
            }
            ;

            foreach (var table in tables)
            {
                try
                {
                    EntityModel rowModel;

                    using (var connection = SqlConnections.New(conn.ConnectionString, conn.ProviderName))
                    {
                        connection.Open();
                        var    tableName = table.FullName;
                        string schema    = null;
                        if (tableName.IndexOf('.') > 0)
                        {
                            schema    = tableName.Substring(0, tableName.IndexOf('.'));
                            tableName = tableName.Substring(tableName.IndexOf('.') + 1);
                        }

                        rowModel = RowGenerator.GenerateModel(connection, schema, tableName,
                                                              table.Module, table.ConnectionKey, table.Identifier, table.PermissionKey, config);

                        var kdiff3Paths = new[]
                        {
                            config.KDiff3Path,
                            Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "KDiff3\\kdiff3.exe"),
                            Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "KDiff3\\kdiff3.exe"),
                        };

                        CodeFileHelper.Kdiff3Path = kdiff3Paths.FirstOrDefault(File.Exists);

                        if (config.TFSIntegration)
                        {
                            CodeFileHelper.SetupTFSIntegration(config.TFPath);
                        }

                        CodeFileHelper.SetupTSCPath(config.TSCPath);
                        var siteWebProj = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, config.WebProjectFile));

                        new EntityCodeGenerator(rowModel, config, siteWebProj).Run();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            if (config.GenerateService ||
                config.GenerateUI ||
                config.GenerateCustom)
            {
                var siteWebProj = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, config.WebProjectFile));
                var siteWebPath = Path.GetDirectoryName(siteWebProj);
                CodeFileHelper.ExecuteTSC(Path.Combine(siteWebPath, @"Scripts\"), "");
            }

            MessageBox.Show("Code files for the selected table is generated. Please REBUILD SOLUTION before running application, otherwise you may have script errors!");
            GenerateCodeButton.IsEnabled = false;
        }
示例#19
0
 public void Save()
 {
     Connections.Sort((x, y) => x.Key.CompareTo(y.Key));
     CodeFileHelper.CheckoutAndWrite(GeneratorConfig.GetConfigurationFilePath(),
                                     JSON.StringifyIndented(this), false);
 }
示例#20
0
        private void GenerateStyle()
        {
#if !ASPNETMVC
            string contentSite = "wwwroot/Content/site".Replace('/', Path.DirectorySeparatorChar);
#else
            string contentSite = "Content/site".Replace('/', Path.DirectorySeparatorChar);
#endif
            string file = Path.Combine(rootDir, Path.Combine(contentSite,
                                                             "site" + model.DotModule.ToLowerInvariant() + ".less"));

            if (!string.IsNullOrEmpty(model.Module))
            {
                var siteLess = Path.Combine(rootDir, Path.Combine(contentSite, "site.less"));

                if (File.Exists(siteLess))
                {
                    var importLine = "@import \"site." + model.Module.ToLowerInvariant() + ".less\";";
                    var lines      = File.ReadAllLines(siteLess).ToList();
                    if (!lines.Any(x => string.Compare(x ?? "", importLine, StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        var index = lines.FindLastIndex(x =>
                        {
                            return(x.StartsWith("@import", StringComparison.Ordinal) ||
                                   (x.StartsWith("//", StringComparison.Ordinal) && x.Contains("if:", StringComparison.Ordinal)));
                        });

                        if (index < 0)
                        {
                            index = lines.Count;
                        }
                        else
                        {
                            index++;
                        }

                        lines.Insert(index, importLine);
                        CodeFileHelper.CheckoutAndWrite(siteLess, string.Join(Environment.NewLine, lines), false);
                    }
                }
            }

            if (!File.Exists(file))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(file));
                CreateFile("@import \"site.mixins.less\";" + Environment.NewLine, file);
            }

            string code = Templates.Render("Style", model);
            using (var ms = new MemoryStream())
            {
                var firstLine = code.Replace("\r", "", StringComparison.Ordinal)
                                .Split('\n').FirstOrDefault(x => !string.IsNullOrWhiteSpace(x));
                if (!string.IsNullOrWhiteSpace(firstLine))
                {
                    var lines = File.ReadAllLines(file);
                    // don't generate less for dialog multiple times
                    if (lines.Any(x => x.IsTrimmedSame(firstLine)))
                    {
                        return;
                    }
                }

                var old = File.ReadAllBytes(file);
                if (old.Length > 0)
                {
                    ms.Write(old, 0, old.Length);
                }
                using (var sw = new StreamWriter(ms, utf8))
                {
                    sw.Write(code);
                    sw.Flush();

                    CodeFileHelper.CheckoutAndWrite(file, ms.ToArray(), false);
                }
            }
        }
示例#21
0
        public static void AddFileToProject(string projectFile, string codeFile, string dependentUpon = null)
        {
            if (File.Exists(projectFile))
            {
                XElement doc;
                using (var sr = new StreamReader(File.OpenRead(projectFile)))
                    doc = XElement.Parse(sr.ReadToEnd(), LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);

                var ns = doc.GetDefaultNamespace();

                Func <string, XElement> findItemGroupOf = fileName =>
                {
                    var lowerFileName = fileName.ToUpperInvariant();

                    foreach (var group in doc.Elements(ns + "ItemGroup"))
                    {
                        foreach (var c in group.Elements(ns + "Compile")
                                 .Concat(group.Elements(ns + "TypeScriptCompile"))
                                 .Concat(group.Elements(ns + "Content"))
                                 .Concat(group.Elements(ns + "None")))
                        {
                            var value = c.Attribute("Include").Value.ToUpperInvariant();
                            if (value == lowerFileName)
                            {
                                return(group);
                            }

                            if (value.Contains("*.", StringComparison.Ordinal))
                            {
                                var extension = Path.GetExtension(lowerFileName);
                                if (value == Path.GetDirectoryName(lowerFileName) + @"\*" + extension)
                                {
                                    return(group);
                                }
                            }
                        }
                    }

                    return(null);
                };

                if (findItemGroupOf(codeFile) != null)
                {
                    return;
                }

                XElement dependentGroup = null;
                if (dependentUpon != null)
                {
                    dependentGroup = findItemGroupOf(dependentUpon);
                    if (dependentGroup == null)
                    {
                        dependentUpon = null;
                    }
                }

                string contentType;
                if (string.Compare(Path.GetExtension(codeFile), ".cs", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    contentType = "Compile";
                }
                else if (string.Compare(Path.GetExtension(codeFile), ".ts", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    contentType = "TypeScriptCompile";
                }
                else
                {
                    contentType = "Content";
                }

                XElement targetGroup = dependentGroup;
                if (targetGroup == null)
                {
                    foreach (var group in doc.Elements(ns + "ItemGroup"))
                    {
                        var compiles = group.Elements(ns + contentType);
                        if (compiles.Any())
                        {
                            targetGroup = group;
                            break;
                        }
                    }
                }

                if (targetGroup == null)
                {
                    return; // create a group??
                }
                var newElement = new XElement(ns + contentType, new XAttribute("Include", codeFile));

                var   lastElement = targetGroup.Elements().LastOrDefault();
                XText space       = null;
                if (lastElement != null)
                {
                    space = lastElement.PreviousNode as XText;
                }
                if (lastElement != null)
                {
                    if (space != null)
                    {
                        lastElement.AddAfterSelf(new XText(space.Value), newElement);
                    }
                    else
                    {
                        lastElement.AddAfterSelf(newElement);
                    }
                }
                else
                {
                    targetGroup.Add(newElement);
                }

                if (dependentUpon != null)
                {
                    newElement.Add(new XText("  "));
                    newElement.Add(new XElement(ns + "DependentUpon", Path.GetFileName(dependentUpon)));
                }

                using (var ms = new MemoryStream())
                    using (var sw = new StreamWriter(ms, new UTF8Encoding(true)))
                    {
                        sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                        sw.Write(doc.ToString());
                        sw.Flush();
                        var bytes = ms.ToArray();
                        CodeFileHelper.CheckoutAndWrite(projectFile, bytes, false);
                    }
            }
        }