示例#1
0
        internal static CompilerResults GetCompilerResults(ZTWPackage package, bool outputErrors, out bool hasErrors)
        {
            var provider = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters();

            var refs = package.References;

            if (refs != null)
            {
                parameters.ReferencedAssemblies.AddRange(refs.ToArray());
            }

            CompilerResults results = provider.CompileAssemblyFromFile(parameters, package.SetupPath);

            if (!results.Errors.Cast <CompilerError>().IsNullOrEmpty())
            {
                if (outputErrors)
                {
                    foreach (CompilerError error in results.Errors)
                    {
                        Console.WriteLine(error);
                    }
                }

                hasErrors = true;
                return(null);
            }

            hasErrors = false;
            return(results);
        }
示例#2
0
        /// <summary>
        /// Adds a new package following the specified path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static ZTWPackage Add(string path)
        {
            var package = new ZTWPackage(path);

            PackageList.Add(package);

            return(package);
        }
示例#3
0
        public static string GetSetupFile(this ZTWPackage package)
        {
            if (!CheckTesterPathDetermined())
            {
                throw new Exception("You must specify Tester before trying to get the Setup file.");
            }

            return(Path.Combine(Path.GetDirectoryName(TesterPath), "Setups", package.GetSetupFileName()));
        }
示例#4
0
        // ===================================================================================
        // =================              Compile *.cs files                 =================
        // ===================================================================================

        internal static bool GetAssembly(ZTWPackage package, out Assembly assembly, bool outputErrors = true)
        {
            bool            hasErrors;
            CompilerResults results = GetCompilerResults(package, outputErrors, out hasErrors);

            if (hasErrors)
            {
                assembly = null;
                return(false);
            }

            assembly = results.CompiledAssembly;
            return(true);
        }
示例#5
0
        public override void Display()
        {
            Console.Write("SLN File Path: ");
            string path = Console.ReadLine();

            if (path.IsDirectory())
            {
                var files = Directory.GetFiles(path, "*.sln");

                if (files.Length == 0)
                {
                    Console.WriteLine("The provided folder doesn't have any folder!", Color.Red);
                    (Program as UpdatableProgram).NavigateBack();
                    return;
                }

                path = files[0];
            }

            if (!File.Exists(path))
            {
                Console.WriteLine("The file you provided doesn't exists!", Color.Red);
                (Program as UpdatableProgram).NavigateBack();
                return;
            }

            ZTWPackage package = PackageController.Add(path);

            // Then check if we have located Tester project... To append a new item to its csproj
            if (!SetupController.CheckTesterPathDetermined())
            {
                Console.Write("Couldn't determine the Tester csproj file, please, determine it.", Color.Yellow);
                Console.Read();
                return;
            }

            TesterController.GenerateSetupInTester(package);

            Console.Write($"Added package '{package.Name}' succesfully! Press any key to go back...", Color.DarkGreen);
            Console.Read();

            (Program as UpdatableProgram).NavigateBack();
        }
示例#6
0
        public static void GenerateSetupInTester(ZTWPackage pkg)
        {
            string name = pkg.Name;

            if (pkg == null)
            {
                throw new ArgumentNullException("pkg");
            }

            string testerPath = SetupController.TesterPath;

            if (!SetupController.CheckTesterPathDetermined())
            {
                throw new Exception("Tester path is null. It must be specified by going to 'Enter Package Creator -> Locate Tester Path'.");
            }

            if (!testerPath.IsDirectory() && Path.GetExtension(testerPath) == "csproj" || testerPath.IsDirectory())
            {
                throw new Exception("The stored Tester isn't a csproj file.");
            }

            // First, we will create the template && store it on a file

            // Declare the Hello world expression
            CodeSnippetExpression helloWorld = new CodeSnippetExpression(@"Console.WriteLine(""Hello world!"")");

            // Declare the class && the "OnSetup" method with its expression
            var c = new CodeTypeDeclaration(pkg.SetupClass)
            {
                Attributes = MemberAttributes.Public,
                IsClass    = true,
                Members    =
                {
                    new CodeMemberMethod()
                    {
                        Name       = PackageConsts.OnSetupMethod,
                        Attributes = MemberAttributes.Public | MemberAttributes.Static,
                        Statements ={ new CodeExpressionStatement(helloWorld)                  }
                    },
                    new CodeMemberMethod()
                    {
                        Name       = PackageConsts.OnFinishMethod,
                        Attributes = MemberAttributes.Public | MemberAttributes.Static,
                        Statements ={ new CodeExpressionStatement(helloWorld)                  }
                    }
                },
                StartDirectives =
                {
                    new CodeRegionDirective(CodeRegionMode.Start, "\nstatic")
                },
                EndDirectives =
                {
                    new CodeRegionDirective(CodeRegionMode.End, string.Empty)
                }
            };

            // Specify and add Namespace
            var ns = new CodeNamespace(pkg.SetupNamespace)
            {
                Types = { c }
            };

            // Create && add "System" import into existing namespace
            ns.Imports.Add(new CodeNamespaceImport("System"));

            // Then, create the unit && add everything into current namespace
            var cu = new CodeCompileUnit()
            {
                Namespaces = { ns }
            };

            // Specify the language
            var provider = CodeDomProvider.CreateProvider("CSharp");

            // Output generated code to string Builder
            var sb = new StringBuilder();

            using (var sourceWriter = new StringWriter(sb))
                provider.GenerateCodeFromCompileUnit(cu, sourceWriter, new CodeGeneratorOptions());

            var text = sb.ToString();

            string testerFolderPath = Path.GetDirectoryName(testerPath),
                   saveFilePath     = Path.Combine(testerFolderPath, "Setups", pkg.PrettyName + ".cs"),
                   saveFolderPath   = Path.GetDirectoryName(saveFilePath);

            if (!Directory.Exists(saveFolderPath))
            {
                Directory.CreateDirectory(saveFolderPath);
            }

            // Overwrite confirmation
            bool overwrite = true;

            if (File.Exists(saveFilePath))
            {
                overwrite = SmartInput.NextConfirm("Do you want to overwrite the current file?");
            }

            if (overwrite)
            {
                File.WriteAllText(saveFilePath, text);
            }

            // Then, we will add the item && save it on the csproj

            RoslynHelper.AddItem(testerPath, testerFolderPath, saveFilePath);
        }
示例#7
0
 public static string GetSetupFileName(this ZTWPackage package)
 {
     return(package.PrettyName + ".cs");
 }
示例#8
0
 public PackageRemove SetPackage(ZTWPackage package)
 {
     CurrentPackage = package;
     return(this);
 }
示例#9
0
        internal static CompilerResults GetCompilerResults(ZTWPackage package, bool outputErrors)
        {
            bool hasErrors;

            return(GetCompilerResults(package, outputErrors, out hasErrors));
        }