CheckoutAndWrite() public static method

public static CheckoutAndWrite ( string file, byte contents, bool addToSourceControl ) : void
file string
contents byte
addToSourceControl bool
return void
コード例 #1
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);
        }
コード例 #2
0
ファイル: EntityCodeGenerator.cs プロジェクト: t0903/Serenity
 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);
     }
 }
コード例 #3
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);
            }
        }
コード例 #4
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
        }
コード例 #5
0
        private void GenerateCss()
        {
            string relativeFile = Path.Combine(@"Content\site\", "site" +
                                               (!string.IsNullOrEmpty(model.Module) ? ("." + model.Module.ToLowerInvariant()) : "") + ".less");

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

            Directory.CreateDirectory(Path.GetDirectoryName(file));
            if (!File.Exists(file))
            {
                if (!string.IsNullOrEmpty(model.Module))
                {
                    relativeFile = Path.Combine(@"Content\site\", "site.less");
                    file         = Path.Combine(siteWebPath, relativeFile);
                }

                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))
                {
                    sw.Write(code);
                    sw.Flush();

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

            ProjectFileHelper.AddFileToProject(siteWebProj, relativeFile);
        }
コード例 #6
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);
                }
            }
        }
コード例 #7
0
 public void Save()
 {
     Connections.Sort((x, y) => x.Key.CompareTo(y.Key));
     CodeFileHelper.CheckoutAndWrite(GeneratorConfig.GetConfigurationFilePath(),
                                     JSON.StringifyIndented(this), false);
 }
コード例 #8
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);
                }
            }
        }
コード例 #9
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);
                    }
            }
        }