Exemplo n.º 1
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.ModelProjectFile));
            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;
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 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");

            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 + "\"");
        }
Exemplo n.º 5
0
 public void Save()
 {
     Connections.Sort((x, y) => x.Key.CompareTo(y.Key));
     CodeFileHelper.CheckoutAndWrite(GeneratorConfig.GetConfigurationFilePath(),
                                     JSON.StringifyIndented(this), false);
 }
Exemplo n.º 6
0
        public static void AddFileToProject(string projectFile, string codeFile, string dependentUpon = null)
        {
            if (File.Exists(projectFile))
            {
                XElement doc;
                using (var sr = new StreamReader(projectFile))
                    doc = XElement.Parse(sr.ReadToEnd(), LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);

                var ns = doc.GetDefaultNamespace();

                Func <string, XElement> findItemGroupOf = fileName =>
                {
                    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")))
                        {
                            if (c.Attribute("Include").Value.ToLowerInvariant() == fileName.ToLowerInvariant())
                            {
                                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") == 0)
                {
                    contentType = "Compile";
                }
                else if (String.Compare(Path.GetExtension(codeFile), ".ts") == 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.Count() > 0)
                        {
                            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);
                    }
            }
        }