Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public string GetRootNamespaceFor(string csproj)
        {
            if (!string.IsNullOrEmpty(RootNamespace))
            {
                return(RootNamespace);
            }

            string rootNamespace = null;

            if (File.Exists(csproj))
            {
                rootNamespace = ProjectFileHelper.ExtractPropertyFrom(csproj,
                                                                      xe => xe.Descendants("RootNamespace").FirstOrDefault()?.Value.TrimToNull());
            }

            if (rootNamespace == null)
            {
                rootNamespace = Path.ChangeExtension(Path.GetFileName(csproj), null);
            }

            if (rootNamespace?.EndsWith(".Web", StringComparison.OrdinalIgnoreCase) == true)
            {
                rootNamespace = rootNamespace.Substring(0, rootNamespace.Length - 4);
            }

            return(rootNamespace);
        }
Exemplo n.º 3
0
        private static void EnumerateProjectDeps(string csproj, Action <string, string, string> dependency)
        {
            var csprojElement = XElement.Parse(File.ReadAllText(csproj));

            foreach (var itemGroup in csprojElement.Descendants("ItemGroup"))
            {
                var condition = itemGroup.Attribute("Condition");
                var target    = "";
                if (condition != null && !string.IsNullOrEmpty(condition.Value))
                {
                    const string tf  = "'$(TargetFramework)' == '";
                    var          idx = condition.Value.IndexOf(tf, StringComparison.Ordinal);
                    if (idx >= 0)
                    {
                        var end = condition.Value.IndexOf("'", idx + tf.Length, StringComparison.Ordinal);
                        if (end >= 0)
                        {
                            target = condition.Value.Substring(idx + tf.Length, end - idx - tf.Length);
                        }
                    }
                }

                foreach (var packageReference in itemGroup.Descendants("PackageReference"))
                {
                    var ver = packageReference.Attribute("Version")?.Value?.Trim();
                    if (!string.IsNullOrEmpty(ver) &&
                        !Version.TryParse(ver, out _) &&
                        ver.StartsWith("$(", StringComparison.Ordinal) &&
                        ver.EndsWith(")", StringComparison.Ordinal))
                    {
                        var prop = ProjectFileHelper.ExtractPropertyFrom(csproj, xel =>
                                                                         xel.Descendants(ver.Substring(2, ver.Length - 3))?
                                                                         .LastOrDefault(x => Version.TryParse(x.Value?.TrimToEmpty(), out _))
                                                                         .Value?.TrimToNull());
                        if (prop != null)
                        {
                            ver = prop;
                        }
                    }

                    dependency(target, packageReference.Attribute("Include").Value, ver);
                }
            }

            if (!Path.GetFileName(csproj).Equals("Directory.Build.props", StringComparison.OrdinalIgnoreCase))
            {
                var dir = Path.GetDirectoryName(Path.GetFullPath(csproj));
                while (!string.IsNullOrEmpty(dir) && Directory.Exists(dir))
                {
                    var dirProps = Path.Combine(dir, "Directory.Build.props");
                    if (File.Exists(dirProps))
                    {
                        EnumerateProjectDeps(dirProps, dependency);
                    }
                    dir = Path.GetDirectoryName(dir);
                }
            }
        }
Exemplo n.º 4
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.º 5
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);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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
        }
Exemplo n.º 8
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
        }
Exemplo n.º 9
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))
            {
                using (var sw = new StreamWriter(file, false, utf8))
                    sw.Write("\r\n");
            }

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

            using (var sw = new StreamWriter(file, true, utf8))
            {
                AppendComment(sw);
                sw.Write(code);
            }

            ProjectFileHelper.AddFileToProject(siteWebProj, relativeFile);
        }
Exemplo n.º 10
0
        public static void Run(string csproj, List <ExternalType> tsTypes)
        {
            var projectDir = Path.GetDirectoryName(csproj);
            var config     = GeneratorConfig.LoadFromFile(Path.Combine(projectDir, "sergen.json"));

            string[] assemblyFiles = null;

            if (config.ServerTypings == null ||
                config.ServerTypings.Assemblies.IsEmptyOrNull())
            {
                var targetFramework = ProjectFileHelper.ExtractTargetFrameworkFrom(csproj);

                if (string.IsNullOrEmpty(targetFramework))
                {
                    Console.Error.WriteLine("Couldn't read TargetFramework from " +
                                            "project file for server typings generation!");
                    Environment.Exit(1);
                }

                string outputName = ProjectFileHelper.ExtractAssemblyNameFrom(csproj)
                                    ?? Path.ChangeExtension(Path.GetFileName(csproj), null);

                var outputExtension = ".dll";
                if (targetFramework.StartsWith("net", StringComparison.OrdinalIgnoreCase) &&
                    !targetFramework.StartsWith("netcoreapp", StringComparison.Ordinal) &&
                    targetFramework.IndexOf('.', StringComparison.Ordinal) < 0)
                {
                    outputExtension = ".exe";
                }

                var outputPath1 = Path.Combine(Path.GetDirectoryName(csproj), "bin/Debug/" + targetFramework + "/" + outputName + outputExtension)
                                  .Replace('/', Path.DirectorySeparatorChar);
                var outputPath2 = Path.Combine(Path.GetDirectoryName(csproj), "bin/Release/" + targetFramework + "/" + outputName + outputExtension)
                                  .Replace('/', Path.DirectorySeparatorChar);

                if (File.Exists(outputPath1))
                {
                    if (File.Exists(outputPath2) &&
                        File.GetLastWriteTime(outputPath1) < File.GetLastWriteTime(outputPath2))
                    {
                        assemblyFiles = new[] { outputPath2 }
                    }
                    ;
                    else
                    {
                        assemblyFiles = new[] { outputPath1 }
                    };
                }
                else if (File.Exists(outputPath2))
                {
                    assemblyFiles = new[] { outputPath2 }
                }
                ;
                else
                {
                    Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                          "Couldn't find output file for server typings generation at {0}!" + Environment.NewLine +
                                                          "Make sure project is built successfully before running Sergen", outputPath1));
                    Environment.Exit(1);
                }
            }

            if (assemblyFiles == null)
            {
                if (config.ServerTypings == null)
                {
                    Console.Error.WriteLine("ServerTypings is not configured in sergen.json file!");
                    Environment.Exit(1);
                }

                if (config.ServerTypings.Assemblies.IsEmptyOrNull())
                {
                    Console.Error.WriteLine("ServerTypings has no assemblies configured in sergen.json file!");
                    Environment.Exit(1);
                }

                assemblyFiles = config.ServerTypings.Assemblies;
                for (var i = 0; i < assemblyFiles.Length; i++)
                {
                    var    assemblyFile1 = Path.GetFullPath(assemblyFiles[i].Replace('/', Path.DirectorySeparatorChar));
                    var    binDebugIdx   = assemblyFile1.IndexOf("/bin/Debug/", StringComparison.OrdinalIgnoreCase);
                    string assemblyFile2 = assemblyFile1;
                    if (binDebugIdx >= 0)
                    {
                        assemblyFile2 = assemblyFile1.Substring(0, binDebugIdx) + "/bin/Release/" + assemblyFile1[(binDebugIdx + "/bin/Release".Length)..];
Exemplo n.º 11
0
        public void Run(string csproj, List <ExternalType> tsTypes)
        {
            var projectDir = Path.GetDirectoryName(csproj);
            var config     = GeneratorConfig.LoadFromFile(Path.Combine(projectDir, "sergen.json"));

            string[] assemblyFiles = null;

            if (config.ServerTypings == null ||
                config.ServerTypings.Assemblies.IsEmptyOrNull())
            {
                var targetFramework = ProjectFileHelper.ExtractTargetFrameworkFrom(csproj);

                if (string.IsNullOrEmpty(targetFramework))
                {
                    Console.Error.WriteLine("Couldn't read TargetFramework from " +
                                            "project file for server typings generation!");
                    Environment.Exit(1);
                }

                string outputName = ProjectFileHelper.ExtractAssemblyNameFrom(csproj)
                                    ?? Path.ChangeExtension(Path.GetFileName(csproj), null);

                var outputExtension = ".dll";
                if (targetFramework.StartsWith("net", StringComparison.OrdinalIgnoreCase) &&
                    !targetFramework.StartsWith("netcoreapp", StringComparison.Ordinal) &&
                    targetFramework.IndexOf('.', StringComparison.Ordinal) < 0)
                {
                    outputExtension = ".exe";
                }

                var outputPath1 = Path.Combine(Path.GetDirectoryName(csproj), "bin/Debug/" + targetFramework + "/" + outputName + outputExtension)
                                  .Replace('/', Path.DirectorySeparatorChar);
                var outputPath2 = Path.Combine(Path.GetDirectoryName(csproj), "bin/Release/" + targetFramework + "/" + outputName + outputExtension)
                                  .Replace('/', Path.DirectorySeparatorChar);

                if (File.Exists(outputPath1))
                {
                    if (File.Exists(outputPath2) &&
                        File.GetLastWriteTime(outputPath1) < File.GetLastWriteTime(outputPath2))
                    {
                        assemblyFiles = new[] { outputPath2 }
                    }
                    ;
                    else
                    {
                        assemblyFiles = new[] { outputPath1 }
                    };
                }
                else if (File.Exists(outputPath2))
                {
                    assemblyFiles = new[] { outputPath2 }
                }
                ;
                else
                {
                    Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                                          "Couldn't find output file for server typings generation at {0}!" + Environment.NewLine +
                                                          "Make sure project is built successfully before running Sergen", outputPath1));
                    Environment.Exit(1);
                }
            }

            if (assemblyFiles == null)
            {
                if (config.ServerTypings == null)
                {
                    Console.Error.WriteLine("ServerTypings is not configured in sergen.json file!");
                    Environment.Exit(1);
                }

                if (config.ServerTypings.Assemblies.IsEmptyOrNull())
                {
                    Console.Error.WriteLine("ServerTypings has no assemblies configured in sergen.json file!");
                    Environment.Exit(1);
                }

                assemblyFiles = config.ServerTypings.Assemblies;
                for (var i = 0; i < assemblyFiles.Length; i++)
                {
                    var    assemblyFile1 = Path.GetFullPath(assemblyFiles[i].Replace('/', Path.DirectorySeparatorChar));
                    var    binDebugIdx   = assemblyFile1.IndexOf("/bin/Debug/", StringComparison.OrdinalIgnoreCase);
                    string assemblyFile2 = assemblyFile1;
                    if (binDebugIdx >= 0)
                    {
                        assemblyFile2 = assemblyFile1.Substring(0, binDebugIdx) + "/bin/Release/" + assemblyFile1.Substring(binDebugIdx + "/bin/Release".Length);
                    }

                    assemblyFiles[i] = assemblyFile1;

                    if (File.Exists(assemblyFile1))
                    {
                        if (File.Exists(assemblyFile2) &&
                            File.GetLastWriteTime(assemblyFile1) < File.GetLastWriteTime(assemblyFile2))
                        {
                            assemblyFiles[i] = assemblyFile2;
                        }
                    }
                    else if (File.Exists(assemblyFile2))
                    {
                        assemblyFiles[i] = assemblyFile2;
                    }
                    else
                    {
                        Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture, string.Format(CultureInfo.CurrentCulture,
                                                                                                        "Assembly file '{0}' specified in sergen.json is not found! " +
                                                                                                        "This might happen when project is not successfully built or file name doesn't match the output DLL." +
                                                                                                        "Please check path in sergen.json and try again.", assemblyFile1)));
                        Environment.Exit(1);
                    }
                }
            }

            if (config.RootNamespace.IsEmptyOrNull())
            {
                config.RootNamespace = config.GetRootNamespaceFor(csproj);
            }

            var generator = new ServerTypingsGenerator(assemblyFiles.ToArray())
            {
                LocalTexts = config.ServerTypings != null && config.ServerTypings.LocalTexts
            };

            var appSettings = Path.Combine(projectDir, "appsettings.json");

            if (generator.LocalTexts && File.Exists(appSettings))
            {
                try
                {
                    var obj      = JObject.Parse(File.ReadAllText(appSettings));
                    var packages = (obj["AppSettings"] as JObject)?["LocalTextPackages"] as JObject;
                    if (packages != null)
                    {
                        foreach (var p in packages.PropertyValues())
                        {
                            foreach (var x in p.Values <string>())
                            {
                                generator.LocalTextFilters.Add(x);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error occurred while parsing appsettings.json!" + Environment.NewLine +
                                      ex.ToString());
                }
            }

            var outDir = Path.Combine(projectDir, (config.ServerTypings?.OutDir.TrimToNull() ?? "Imports/ServerTypings")
                                      .Replace('/', Path.DirectorySeparatorChar));

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("Transforming ServerTypings at: ");
            Console.ResetColor();
            Console.WriteLine(outDir);

            generator.RootNamespaces.Add(config.RootNamespace);

            foreach (var type in tsTypes)
            {
                generator.AddTSType(type);
            }

            var codeByFilename = generator.Run();

            new MultipleOutputHelper().WriteFiles(outDir, codeByFilename, "*.ts");
        }