Exemplo n.º 1
0
        protected IProject ProjectCreator()
        {
            var dialog = new NameDialog();

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                var name        = dialog.EnteredText;
                var solutionDir = FSolution.Location.GetLocalDir();
                var projectPath = solutionDir.ConcatPath(name).ConcatPath(name + ".csproj");
                var location    = new Uri(projectPath);
                var project     = new CSProject(Path.GetFileName(location.LocalPath), location);
                if (File.Exists(project.Location.LocalPath))
                {
                    project.Load();
                }
                else
                {
                    project.Save();
                }
                return(project);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        private bool RecompileIfNeeded(CSProject project)
        {
            if (!IsAssemblyUpToDate(project))
            {
                FLogger.Log(LogType.Message, "Assembly of {0} is not up to date. Need to recompile ...", project.Name);

                var isLoaded = project.IsLoaded;
                if (!isLoaded)
                {
                    project.Load();
                }

                project.ProjectCompiledSuccessfully -= project_ProjectCompiled;
                project.Compile();
                project.ProjectCompiledSuccessfully += project_ProjectCompiled;

                if (!isLoaded)
                {
                    project.Unload();
                }

                if (project.CompilerResults.Errors.HasErrors)
                {
                    FLogger.Log(LogType.Error, GetCompileErrorsLog(project, project.CompilerResults));
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 3
0
 void AddOrUpdateAssembly(CSProject cs)
 {
     if (cs.AssemblyLocation != null)
     {
         foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
         {
             try
             {
                 if ((!a.IsDynamic) && a.Location == cs.AssemblyLocation)
                 {
                     if (FCSProjects.ContainsKey(cs))
                     {
                         UpdateAssembly(cs, a);
                     }
                     else
                     {
                         FCSProjects.Add(cs, a);
                     }
                 }
             }
             catch (Exception e)
             {
                 System.Diagnostics.Debug.WriteLine(e);
             }
         }
     }
 }
Exemplo n.º 4
0
        public override string Generate(ProjectBase Project, bool WithBeutyConfigurationName)
        {
            CSProject project = (CSProject)Project;

            CSProject.Profile profile = (CSProject.Profile)project.Profiles[0];

            XmlElement projectElement = CreateProjectElement();

            {
                XmlElement propertyGroup = CreateElement("PropertyGroup", projectElement);
                {
                    SetElementValue("AssemblyName", profile.AssemblyName, propertyGroup);
                    SetElementValue("OutputPath", profile.OutputPath, propertyGroup);
                    SetElementValue("OutputType", GetOutputType(profile), propertyGroup);
                    SetElementValue("DefineConstants", GetFlattenStringList(profile.PreprocessorDefinitions), propertyGroup);
                    SetElementValue("TargetFrameworkVersion", profile.FrameworkVersion.ToString().Replace('_', '.'), propertyGroup);
                }

                XmlElement target = CreateElement("Target", projectElement);
                {
                    target.SetAttribute("Name", "Build");

                    XmlElement makeDir = CreateElement("MakeDir", target);
                    {
                        makeDir.SetAttribute("Directories", "$(OutputPath)");
                        makeDir.SetAttribute("Condition", "!Exists('$(OutputPath)')");

                        makeDir = CreateElement("Csc", target);
                        makeDir.SetAttribute("Sources", "@(Compile)");
                        makeDir.SetAttribute("OutputAssembly", "$(OutputPath)$(AssemblyName)");

                        target = CreateElement("Target", projectElement);
                        {
                            target.SetAttribute("Name", "Clean");

                            makeDir = CreateElement("Delete", target);
                            makeDir.SetAttribute("Files", "$(OutputPath)$(AssemblyName)");

                            target = CreateElement("Target", projectElement);
                            {
                                target.SetAttribute("Name", "Rebuild");
                                target.SetAttribute("DependsOnTargets", "Clean;Build");
                            }
                        }
                    }

                    XmlElement import = CreateElement("Import", projectElement);
                    import.SetAttribute("Project", "$(MSBuildToolsPath)/Microsoft.CSharp.targets");

                    XmlElement referencesItemGroup = CreateElement("ItemGroup", projectElement);
                    AddStringListToEllementAsAttribute(referencesItemGroup, "Reference", "Include", project.ReferenceBinaryFiles);

                    XmlElement compilesItemGroup = CreateElement("ItemGroup", projectElement);
                    AddStringListToEllementAsAttribute(compilesItemGroup, "Compile", "Include", project.CompileFiles);
                }
            }

            return(projectElement.OwnerDocument.OuterXml);
        }
Exemplo n.º 5
0
        private bool IsAssemblyUpToDate(CSProject project)
        {
            if (project.AssemblyLocation == null)
            {
                return(false);
            }

            var now         = DateTime.Now;
            var projectTime = new [] { File.GetLastWriteTime(project.LocalPath) }
            .Concat(project.Documents.Select(d => File.GetLastWriteTime(d.LocalPath)))
            .Max();
            var assemblyTime = File.GetLastWriteTime(project.AssemblyLocation);

            // This can happen in case the computer time is wrong or
            // in a different time zone than the project was created in.
            if (now < projectTime)
            {
                projectTime = now - TimeSpan.FromSeconds(10.0);
            }

            if (File.Exists(project.AssemblyLocation) && (projectTime <= assemblyTime))
            {
                // We also need to check if the version info of the referenced assemblies
                // is the same as the one referenced by the project file.
                // We only check the PluginInterfaces assembly here to save performance.
                var assembly   = Assembly.ReflectionOnlyLoadFrom(project.AssemblyLocation);
                var piAssembly = assembly.GetReferencedAssemblies().Where(assemblyName => assemblyName.Name == typeof(IPluginBase).Assembly.GetName().Name).FirstOrDefault();

                if (piAssembly != null && piAssembly.Version != FPluginInterfacesVersion)
                {
                    return(false);
                }

                switch (project.BuildConfiguration)
                {
                case BuildConfiguration.Release:
                    return(!IsAssemblyDebugBuild(assembly));

                case BuildConfiguration.Debug:
                    return(IsAssemblyDebugBuild(assembly));

                default:
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 6
0
        private CSProject CreateProject(string filename)
        {
            var project = FSolution.Projects[filename] as CSProject;

            if (project == null)
            {
                var binDir = Path.GetDirectoryName(filename).ConcatPath("bin").ConcatPath("Dynamic");
                DeleteArtefacts(binDir);

                project = new CSProject(filename);
                FSolution.Projects.Add(project);
                project.ProjectCompiledSuccessfully += project_ProjectCompiled;
                project.CompileCompleted            += project_CompileCompleted;
            }
            return(project);
        }
Exemplo n.º 7
0
 void UpdateAssembly(CSProject cs, Assembly a)
 {
     FCSProjects[cs] = a;
     foreach (var kv in FTypeToProject)
     {
         if (kv.Value == cs)
         {
             var t = a.GetType(kv.Key, false, true);
             if (t != null)
             {
                 FMappings[kv.Key] = t;
                 TypeUpdated?.Invoke(this, kv.Key);
             }
         }
     }
 }
Exemplo n.º 8
0
        private bool BuildWrapperLibrary(List <string> WrapperFiles)
        {
            const string ProjectName = "Wrapper";

            string projectDir = EnvironmentHelper.IntermediateDirectory + ProjectName + EnvironmentHelper.PathSeparator;

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

            CSProject csproj = new CSProject();

            CSProject.Profile profile = (CSProject.Profile)csproj.CreateProfile();

            profile.FrameworkVersion = CSProject.Profile.FrameworkVersions.v4_5;
            profile.AssemblyName     = ProjectName;
            profile.OutputPath       = projectDir + "Build" + EnvironmentHelper.PathSeparator;
            profile.IntermediatePath = projectDir;
            profile.OutputType       = ProjectBase.ProfileBase.OutputTypes.DynamicLinkLibrary;

            DateTime startTime = DateTime.Now;

            ConsoleHelper.WriteInfo("Building wrapper starts at " + startTime.ToString());

            if (WrapperFiles.Count == 0)
            {
                ConsoleHelper.WriteInfo("No building rules found, aborting process");
                return(false);
            }

            foreach (string file in WrapperFiles)
            {
                csproj.AddCompileFile(file);
            }

            if (compiler.Build(profile))
            {
                return(true);
            }

            ConsoleHelper.WriteInfo("Building wrapper takes " + (DateTime.Now - startTime).ToHHMMSS());

            return(false);
        }
Exemplo n.º 9
0
        protected static CSDocument FindDefiningDocument(CSProject project, INodeInfo nodeInfo)
        {
            foreach (var doc in project.Documents)
            {
                var csDoc = doc as CSDocument;
                if (csDoc == null)
                {
                    continue;
                }

                if (FindDefiningLine(csDoc, nodeInfo) >= 0)
                {
                    return(csDoc);
                }
            }

            return(null);
        }
Exemplo n.º 10
0
        protected IProject ProjectCreator()
        {
            var dialog = new NameDialog();

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                var name        = dialog.EnteredText;
                var solutionDir = Path.GetDirectoryName(FSolution.LocalPath);
                var projectPath = solutionDir.ConcatPath(name).ConcatPath(name + ".csproj");
                var project     = new CSProject(projectPath);
                if (!File.Exists(project.LocalPath))
                {
                    project.Save();
                }
                return(project);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 11
0
        protected override bool CloneNode(INodeInfo nodeInfo, string path, string name, string category, string version, out string newFilename)
        {
            string className = string.Format("{0}{1}{2}Node", version, category, name);

            className = Regex.Replace(className, @"[^a-zA-Z0-9]+", "_");
            var regexp = new Regex(@"^[0-9]+");

            if (regexp.IsMatch(className))
            {
                className = string.Format("C{0}", className);
            }

            // Find a suitable project name
            var newProjectName = string.Format("{0}{1}{2}", version, category, name);;
            var newProjectPath = path.ConcatPath(newProjectName).ConcatPath(newProjectName + ".csproj");

            int    i = 1;
            string tmpNewProjectName = newProjectName;
            string tmpClassName      = className;

            while (File.Exists(newProjectPath))
            {
                newProjectName = tmpNewProjectName + i;
                className      = tmpClassName + i++;
                newProjectPath = path.ConcatPath(newProjectName).ConcatPath(newProjectName + ".csproj");
            }

            var filename = nodeInfo.Filename;
            var project  = CreateProject(filename);

            project.SaveTo(newProjectPath);

            using (var newProject = new CSProject(newProjectPath))
            {
                foreach (var doc in newProject.Documents)
                {
                    var csDoc = doc as CSDocument;
                    if (csDoc != null)
                    {
                        // Rename the CSDocument
                        if (ContainsNodeInfo(csDoc, nodeInfo))
                        {
                            var newDocName = string.Format("{0}.cs", Path.GetFileNameWithoutExtension(className));
                            csDoc.Name = newDocName;
                            csDoc.Rename(newDocName);
                            break;
                        }
                    }
                }

                foreach (var doc in newProject.Documents)
                {
                    // Now scan the document for possible plugin infos.
                    // If we find one, update its properties and rename the class.
                    var csDoc = doc as CSDocument;
                    if (csDoc != null)
                    {
                        var parserResults   = csDoc.Parse(true);
                        var compilationUnit = parserResults.CompilationUnit;

                        // Write new values to plugin info and remove all other plugin infos.
                        var pluginInfoTransformer = new PluginClassTransformer(nodeInfo, name, category, version, className);
                        compilationUnit.AcceptVisitor(pluginInfoTransformer, null);

                        var outputVisitor = new CSharpOutputVisitor();
                        var specials      = parserResults.Specials;

                        using (SpecialNodesInserter.Install(specials, outputVisitor))
                        {
                            outputVisitor.VisitCompilationUnit(compilationUnit, null);
                        }

                        csDoc.TextContent = outputVisitor.Text;
                    }
                }

                // Save the project.
                newProject.Save();

                newFilename = newProject.LocalPath;
                return(true);
            }
        }
Exemplo n.º 12
0
        public bool Build()
        {
            string projectDir = EnvironmentHelper.IntermediateDirectory + ProjectName + EnvironmentHelper.PathSeparator;

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

            CSProject csproj = new CSProject();

            CSProject.Profile profile = (CSProject.Profile)csproj.CreateProfile();

            profile.FrameworkVersion = CSProject.Profile.FrameworkVersions.v4_5;
            profile.AssemblyName     = ProjectName;
            profile.OutputPath       = projectDir + "Build" + EnvironmentHelper.PathSeparator;
            profile.IntermediatePath = projectDir;
            profile.OutputType       = ProjectBase.ProfileBase.OutputTypes.DynamicLinkLibrary;
            csproj.AddReferenceBinaryFile(Assembly.GetExecutingAssembly().Location);

            string[] files = FileSystemUtilites.GetAllFiles(processDirectory, "*" + BuildRules.BuildRuleFilePostfix);

            DateTime startTime = DateTime.Now;

            ConsoleHelper.WriteInfo("Building rules starts at " + startTime.ToString());
            ConsoleHelper.WriteInfo("Found rules :");

            if (files.Length == 0)
            {
                ConsoleHelper.WriteInfo("No building rules found, aborting process");
                return(false);
            }

            foreach (string rules in files)
            {
                csproj.AddCompileFile(rules);

                ConsoleHelper.WriteInfo("\t" + Path.GetFileName(rules));
            }

            Compiler compiler = new Compiler();

            compiler.ErrorRaised += OnError;

            if (compiler.Build(profile))
            {
                Assembly rulesLibrary = Assembly.LoadFile(profile.OutputPath + ProjectName + EnvironmentHelper.DynamicLibraryExtentions);

                List <string>     buildRulesFiles = new List <string>();
                List <BuildRules> buildRules      = new List <BuildRules>();

                foreach (string buildRuleName in files)
                {
                    string fileName = Path.GetFileNameWithoutExtension(buildRuleName);
                    string typeName = fileName.Replace(".", "");
                    Type   type     = rulesLibrary.GetType(BuildRules.NamespacePrefix + typeName);

                    if (type == null)
                    {
                        ConsoleHelper.WriteWarning("In " + fileName + ", type " + typeName + " doesn't exists, building related module will be ignore");
                        continue;
                    }

                    buildRulesFiles.Add(buildRuleName);

                    BuildRules buildRule = (BuildRules)Activator.CreateInstance(type);
                    buildRule.Path = FileSystemUtilites.PathSeperatorCorrection(Path.GetDirectoryName(buildRuleName)) + EnvironmentHelper.PathSeparator;

                    Type[] types = buildRule.GetType().GetNestedTypes();

                    buildRule.Rules = new BuildRules.RuleBase[types.Length];

                    for (int i = 0; i < types.Length; ++i)
                    {
                        buildRule.Rules[i] = (BuildRules.RuleBase)Activator.CreateInstance(types[i]);
                    }

                    buildRules.Add(buildRule);
                }

                RulesFiles = buildRulesFiles.ToArray();
                Rules      = buildRules.ToArray();

                return(true);
            }

            ConsoleHelper.WriteInfo("Building rules takes " + (DateTime.Now - startTime).ToHHMMSS());

            return(false);
        }