public void CountSimple()
        {
            project.LoadXml(ProjectContentWithThreeBuildItemGroups);
            BuildItemGroupCollection groups = project.ItemGroups;

            Assertion.AssertEquals(3, groups.Count);
        }
        public void IsSynchronizedDefault()
        {
            project.LoadXml(ProjectContentWithThreeBuildItemGroups);
            BuildItemGroupCollection groups = project.ItemGroups;

            Assertion.AssertEquals(false, groups.IsSynchronized);
        }
        public void CopyToArrayThatsNotLargeEnough()
        {
            project.LoadXml(ProjectContentWithThreeBuildItemGroups);
            BuildItemGroupCollection groups = project.ItemGroups;

            object[] array = new object[groups.Count - 1];
            groups.CopyTo(array, 0);
        }
        public void CountParentAndImported()
        {
            Project p = GetProjectThatImportsAnotherProject(null, null);

            BuildItemGroupCollection groups = p.ItemGroups;

            Assertion.AssertEquals(5, groups.Count);
        }
        public void CountAfterRemovingAllGroups()
        {
            project.LoadXml(ProjectContentWithThreeBuildItemGroups);
            BuildItemGroupCollection groups = project.ItemGroups;

            project.RemoveAllItemGroups();

            Assertion.AssertEquals(0, groups.Count);
        }
        public void CountAfterAddingNewGroup()
        {
            project.LoadXml(ProjectContentWithThreeBuildItemGroups);
            BuildItemGroupCollection groups = project.ItemGroups;

            project.AddNewItemGroup();

            Assertion.AssertEquals(4, groups.Count);
        }
        public void CountZero()
        {
            string projectContents = @"
                    <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
                    </Project>
                    ";

            project.LoadXml(projectContents);
            BuildItemGroupCollection groups = project.ItemGroups;

            Assertion.AssertEquals(0, groups.Count);
        }
        public void CountImportedOnly()
        {
            string parentProjectContents = @" 
                    <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
                        <Import Project='import.proj' />
                    </Project>
                ";

            Project p = GetProjectThatImportsAnotherProject(null, parentProjectContents);

            BuildItemGroupCollection groups = p.ItemGroups;

            Assertion.AssertEquals(2, groups.Count);
        }
        public void CopyToSimple()
        {
            project.LoadXml(ProjectContentWithThreeBuildItemGroups);
            BuildItemGroupCollection groups = project.ItemGroups;

            object[] array = new object[groups.Count];
            groups.CopyTo(array, 0);

            int count = 0;

            foreach (BuildItemGroup group in groups)
            {
                Assertion.AssertEquals(array[count].ToString(), group.ToString());
                count++;
            }
        }
예제 #10
0
        static Int32 Main(string[] args)
        {
            CopyProjectOptions options = new CopyProjectOptions();
            List<String> nonOptionArgs = options.Parse(args);

            if (nonOptionArgs.Count != 2)
            {
                return options.ErrorAndUsage("Expected 2 non-option arguments but got {0}", nonOptionArgs.Count);
            }

            //
            // Setup source and dest proj names/paths
            //
            String sourceProj = nonOptionArgs[0];
            if (!File.Exists(sourceProj))
            {
                Console.WriteLine("Source csproj file '{0}' does not exist", sourceProj);
                return 1;
            }
            String sourceProjPath = Path.GetDirectoryName(sourceProj);
            String sourceProjName = Path.GetFileName(sourceProj);

            String destProjOrPath = nonOptionArgs[1];
            String destProj, destProjPath;

            if(destProjOrPath.EndsWith(".csproj"))
            {
                destProj = destProjOrPath;
                destProjPath = Path.GetDirectoryName(destProjOrPath);
            }
            else
            {
                destProj = Path.Combine(destProjOrPath, sourceProjName);
                destProjPath = destProjOrPath;
            }

            if (!Directory.Exists(destProjPath))
            {
                Console.WriteLine("Destination Directory '{0}' doesn't exist", destProjPath);
                return 1;
            }

            //
            // Print Settings
            //
            Console.WriteLine();
            Console.WriteLine("-----------------------------------------------------------------------------");
            Console.WriteLine("Settings");
            Console.WriteLine("-----------------------------------------------------------------------------");
            Console.WriteLine("Source Proj File : {0}", sourceProj);
            Console.WriteLine("Dest Proj File   : {0}", destProj);

            //
            //
            //
            Console.WriteLine();
            Console.WriteLine("-----------------------------------------------------------------------------");
            Console.WriteLine("Processing source csproj file...");
            Console.WriteLine("-----------------------------------------------------------------------------");

            HashSet<FileResource> compileFiles = new HashSet<FileResource>();

            /*
            BuildProperty DefaultMSBuildBinPath = null;
            if (sourceProjName.EndsWith(".CE.csproj"))
            {
                if (DefaultMSBuildBinPath == null)
                {
                    DefaultMSBuildBinPath = Engine.GlobalEngine.GlobalProperties["MSBuildBinPath"];
                    //Engine.GlobalEngine.GlobalProperties["MSBuildBinPath"] = new BuildProperty("MSBuildBinPath", @"C:\Windows\Microsoft.NET\Framework\v3.5");
                }
            }
            else
            {
                if (DefaultMSBuildBinPath != null)
                {
                    //Engine.GlobalEngine.GlobalProperties["MSBuildBinPath"] = DefaultMSBuildBinPath;
                    DefaultMSBuildBinPath = null;
                }
            }
            */
            Project sourceProject = new Project();
            sourceProject.Load(sourceProj);

            BuildItemGroupCollection itemGroups = sourceProject.ItemGroups;

            foreach (BuildItemGroup itemGroup in itemGroups)
            {
                //Console.WriteLine("ItemGroup Count='{0}' Condition='{1}'", itemGroup.Count, itemGroup.Condition);

                foreach (BuildItem item in itemGroup)
                {
                    if (item.Name.Equals("Compile"))
                    {
                        String include = item.Include;
                        String exclude = item.Exclude;

                        if(!String.IsNullOrEmpty(include))
                        {
                            FileResource resource = new FileResource(include, sourceProjPath, destProjPath);
                            if (!File.Exists(resource.sourceRelativeToCWDNameAndPath))
                            {
                                Console.WriteLine("Error: ItemGroup 'Compile' Include '{0}' does not exist (name relative to current working directory is '{1}')",
                                    resource.sourceRelativeToCWDNameAndPath, resource.sourceRelativeToCWDNameAndPath);
                                return 1;
                            }

                            Console.WriteLine("Compile Include '{0}'", resource.relativeToProjNameAndPath);
                            compileFiles.Add(resource);
                        }
                        if(!String.IsNullOrEmpty(exclude))
                        {
                            throw new NotImplementedException();
                        }
                    }
                    else
                    {
                        //Console.WriteLine("Ignoring '{0}'", item.Include);
                    }
                }
            }

            //
            // Load template file if applicable
            //
            Project destProject;
            if (!options.templateFile.set)
            {
                destProject = sourceProject;
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("-----------------------------------------------------------------------------");
                Console.WriteLine("Loading csproj template file...");
                Console.WriteLine("-----------------------------------------------------------------------------");
                String templateFile = options.templateFile.ArgValue;
                if(templateFile.StartsWith("src:"))
                {
                    templateFile = Path.Combine(sourceProjPath, templateFile.Substring("src:".Length));
                }
                else if(templateFile.StartsWith("dst:"))
                {
                    templateFile = Path.Combine(destProjPath, templateFile.Substring("dst:".Length));
                }

                if (!File.Exists(templateFile))
                {
                    Console.WriteLine("Error: Template file '{0}' does not exist ('{1}')", options.templateFile.ArgValue,
                        templateFile);
                    return 1;
                }

                destProject = new Project();
                destProject.Load(templateFile);

                //
                // Add compile files to template
                //
                BuildItemGroup compileItemGroup = destProject.AddNewItemGroup();
                foreach (FileResource resource in compileFiles)
                {
                    compileItemGroup.AddNewItem("Compile", resource.relativeToProjNameAndPath);
                }
            }

            //
            //
            //
            List<PropertyChange> propertyChanges = new List<PropertyChange>();

            if (options.assemblyName.set)
            {
                propertyChanges.Add(new PropertyChange("AssemblyName", options.assemblyName.ArgValue));
            }

            if (options.propChanges.set)
            {
                String[] changes = options.propChanges.ArgValue.Split(new Char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < changes.Length; i++)
                {
                    String change = changes[i];

                    Int32 colonIndex = change.IndexOf(':');
                    if (colonIndex < 0)
                    {
                        Console.WriteLine("Invalid -p or --props value '{0}', each semi-colon separated change must be a <name>:<value> pair", options.propChanges.ArgValue);
                        return 1;
                    }
                    propertyChanges.Add(new PropertyChange(change.Remove(colonIndex), change.Substring(colonIndex + 1)));
                }
            }

            if (propertyChanges.Count > 0)
            {
                Console.WriteLine();
                Console.WriteLine("-----------------------------------------------------------------------------");
                Console.WriteLine("Modifying source csproj file...");
                Console.WriteLine("-----------------------------------------------------------------------------");

                // Print changes
                for (int i = 0; i < propertyChanges.Count; i++)
                {
                    PropertyChange change = propertyChanges[i];
                    Console.WriteLine("Property '{0}', new value '{1}'", change.name, change.value);
                }

                foreach (BuildPropertyGroup propertyGroup in destProject.PropertyGroups)
                {
                    foreach (BuildProperty property in propertyGroup)
                    {
                        for (int i = 0; i < propertyChanges.Count; i++)
                        {
                            PropertyChange change = propertyChanges[i];

                            if (property.Name.Equals(change.name))
                            {
                                if (property.IsImported)
                                {
                                    Console.WriteLine("WARNING: Property <{0}>{1}</{0}> is imported and cannot be changed", property.Name, property.Value);
                                }
                                else
                                {
                                    Console.WriteLine("Change: Property '{0}' from '{1}' to '{2}'", change.name, property.Value, change.value);
                                    property.Value = change.value;
                                    change.changeCount++;
                                }
                            }
                        }
                    }
                }

                // Check changes
                for (int i = 0; i < propertyChanges.Count; i++)
                {
                    PropertyChange change = propertyChanges[i];
                    if (change.changeCount <= 0)
                    {
                        Console.WriteLine("Error: Did not find any non-imported <{0}> properties to change", change.name);
                        return 1;
                    }

                    Console.WriteLine("Changed {0} <{1}> properties", change.changeCount, change.name);
                }
            }

            Console.WriteLine();
            Console.WriteLine("-----------------------------------------------------------------------------");
            Console.WriteLine("Saving new csproj file...");
            Console.WriteLine("-----------------------------------------------------------------------------");
            destProject.Save(destProj);

            Console.WriteLine();
            Console.WriteLine("-----------------------------------------------------------------------------");
            Console.WriteLine("Copying Files...");
            Console.WriteLine("-----------------------------------------------------------------------------");

            //
            // Setup copy variables
            //
            String oldDottedNamespace, newDottedNamespace;
            String oldNamespace, newNamespace;

            if(options.namespaceChange.set)
            {
                String namespaceChangeString = options.namespaceChange.ArgValue;
                Int32 colonIndex = namespaceChangeString.IndexOf(':');
                if(colonIndex < 0)
                {
                    Console.WriteLine("Namespace change option should contain a colon to seperate the namespaces.");
                    return 1;
                }
                oldNamespace = namespaceChangeString.Remove(colonIndex);
                newNamespace = namespaceChangeString.Substring(colonIndex + 1);

                oldDottedNamespace = oldNamespace + ".";
                newDottedNamespace = newNamespace + ".";
            }
            else
            {
                oldDottedNamespace = null;
                newDottedNamespace = null;
                oldNamespace = null;
                newNamespace = null;
            }

            //
            // Copy the project file
            //
            foreach (FileResource compileFile in compileFiles)
            {
                //
                // Make any destination directories
                //
                String directory = Path.GetDirectoryName(compileFile.relativeToProjNameAndPath);
                if (!String.IsNullOrEmpty(directory))
                {
                    String destinationRelativeToCWDDirectory = Path.Combine(destProjPath, directory);
                    if (!Directory.Exists(destinationRelativeToCWDDirectory))
                    {
                        Console.WriteLine("Creating destination directory '{0}'", directory);
                        Directory.CreateDirectory(destinationRelativeToCWDDirectory);
                    }
                }

                if (!options.namespaceChange.set)
                {
                    File.Copy(compileFile.sourceRelativeToCWDNameAndPath, compileFile.destRelativeToCWDNameAndPath, true);
                }
                else
                {
                    String fileContents = FileExtensions.ReadFileToString(compileFile.sourceRelativeToCWDNameAndPath);

                    //
                    // Make Replacements
                    //
                    fileContents = fileContents.Replace("namespace " + oldDottedNamespace, "namespace " + newDottedNamespace);
                    fileContents = fileContents.Replace("namespace " + oldNamespace, "namespace " + newNamespace);

                    fileContents = fileContents.Replace("using " + oldDottedNamespace, "using " + newDottedNamespace);
                    fileContents = fileContents.Replace("using " + oldNamespace, "using " + newNamespace);

                    fileContents = fileContents.Replace(oldNamespace + ".", newNamespace + ".");

                    FileExtensions.SaveStringToFile(compileFile.destRelativeToCWDNameAndPath, FileMode.Create, fileContents);
                }
            }

            return 0;
        }