Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="package"></param>
        public void ImportNativeApps(Package package)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            this.package = package;
            string projectLocation = Dte.get_Properties("Environment", "ProjectsAndSolution").Item("ProjectsLocation").Value.ToString();
            string file            = GetFilesDialog(
                "Select CONNECT Edition Make File",
                "Make File (*.mke)|*.mke",
                MSCESDKPath);

            if (null != file)
            {
                FileInfo originalFileInfo = new FileInfo(file);
                InitialDirectory = originalFileInfo.DirectoryName;
                string[] pathSplit        = originalFileInfo.DirectoryName.Split('\\');
                bool     isCanceled       = false;
                string   solutionFile     = null;
                string   solutionFileName = null;
                string   solutionPath     = null;
                if (string.IsNullOrEmpty(Dte.Solution.FullName))
                {
                    solutionFile = GetFilesDialog(
                        "Select Solution or Create New Solution to add Project",
                        "Solution (*.sln)|*.sln",
                        projectLocation,
                        originalFileInfo.Name.Replace(originalFileInfo.Extension, "") + ".sln"
                        );
                    if (!string.IsNullOrEmpty(solutionFile))
                    {
                        try
                        {
                            FileInfo solutionFileInfo = new FileInfo(solutionFile);
                            solutionFileName = solutionFileInfo.Name.Replace(".sln", "");
                            solutionPath     = solutionFileInfo.DirectoryName + "\\" + solutionFileName;
                            Dte.Solution.Open(solutionFile);
                        }
                        catch (Exception)
                        {
                            FileInfo solutionFileInfo = new FileInfo(solutionFile);
                            solutionFileName = solutionFileInfo.Name.Replace(".sln", "");
                            solutionPath     = solutionFileInfo.DirectoryName + "\\" + solutionFileName;
                            Dte.Solution.Create(solutionPath, solutionFileInfo.Name);
                        }
                    }
                    else
                    {
                        isCanceled = true;
                    }
                }
                else
                {
                    FileInfo solutionFileInfo = new FileInfo(Dte.Solution.FullName);
                    solutionFileName = solutionFileInfo.Name.Replace(".sln", "");
                    solutionPath     = solutionFileInfo.DirectoryName;
                    solutionFile     = Dte.Solution.FullName;
                }

                if (!isCanceled)
                {
                    if (!Directory.Exists(solutionPath))
                    {
                        Directory.CreateDirectory(solutionPath);
                        Dte.Solution.SaveAs(solutionPath + "\\" + solutionFileName + ".sln");
                    }

                    if (null != solutionFile)
                    {
                        string vsDir          = Environment.GetEnvironmentVariable("VisualStudioDir");
                        string destinationDir = solutionPath + "\\" + originalFileInfo.Name.Replace(originalFileInfo.Extension, "") + "\\";

                        if (!Directory.Exists(destinationDir))
                        {
                            Directory.CreateDirectory(destinationDir);

                            // Get our files (recursive and any of them, based on the 2nd param of the Directory.GetFiles() method
                            string[] originalFiles = Directory.GetFiles(originalFileInfo.DirectoryName, "*", SearchOption.AllDirectories);
                            Array.ForEach(originalFiles, (originalFileLocation) =>
                            {
                                // Get the FileInfo for both of our files
                                FileInfo originalFile = new FileInfo(originalFileLocation);
                                FileInfo destFile     = new FileInfo(originalFileLocation.Replace(originalFileInfo.DirectoryName, destinationDir));
                                // ^^ We can fill the FileInfo() constructor with files that don't exist...

                                // ... because we check it here
                                if (destFile.Exists)
                                {
                                    // Logic for files that exist applied here; if the original is larger, replace the updated files...
                                    if (originalFile.Length > destFile.Length)
                                    {
                                        originalFile.CopyTo(destFile.FullName, true);
                                    }
                                }
                                else // ... otherwise create any missing directories and copy the folder over
                                {
                                    Directory.CreateDirectory(destFile.DirectoryName); // Does nothing on directories that already exist
                                    originalFile.CopyTo(destFile.FullName, false); // Copy but don't over-write
                                }
                            }
                                          );

                            DirectoryInfo directoryInfo = new DirectoryInfo(destinationDir);

                            string result        = Resources.CONNECTvcproj;
                            string resultFilters = Resources.CONNECTFilters;
                            result = result
                                     .Replace("$safeprojectname$", pathSplit[pathSplit.GetUpperBound(0)])
                                     .Replace("$guid1$", "{" + Guid.NewGuid().ToString() + "}")
                                     .Replace("$platformtoolset$", PlatFormToolSet(Dte.Version));

                            resultFilters = resultFilters
                                            .Replace("$guid1$", "{" + Guid.NewGuid().ToString() + "}")
                                            .Replace("$guid2$", "{" + Guid.NewGuid().ToString() + "}")
                                            .Replace("$guid3$", "{" + Guid.NewGuid().ToString() + "}");

                            foreach (var item in directoryInfo.GetFiles("*", SearchOption.AllDirectories).OrderBy(f => f.Extension))
                            {
                                if (item.Extension.ToLower() == ".cpp" || item.Extension.ToLower() == ".h")
                                {
                                    result += "<ClCompile Include=";
                                }
                                else
                                {
                                    result += "<ClInclude Include=";
                                }
                                result += "\"" + item.FullName.Replace(destinationDir, "") + "\"/>\n";
                            }
                            if (directoryInfo.GetDirectories().Count() >= 0)
                            {
                                foreach (var directories in directoryInfo.GetDirectories())
                                {
                                    resultFilters += "<ItemGroup>\n<Filter Include=\"" + directories.Name + "\">\n" +
                                                     "<UniqueIdentifier>{" + Guid.NewGuid().ToString() + "}</UniqueIdentifier>\n" +
                                                     "</Filter>\n" +
                                                     "</ItemGroup>\n";

                                    foreach (var files in directoryInfo.GetFiles("*", SearchOption.AllDirectories).OrderBy(f => f.Extension))
                                    {
                                        if (files.FullName.Contains(directories.Name))
                                        {
                                            if (files.Extension.ToLower() == ".cpp" || files.Extension.ToLower() == ".h")
                                            {
                                                resultFilters += "<ItemGroup>\n" +
                                                                 "<ClCompile Include=\"" + files.FullName.Replace(destinationDir, "") + "\">\n" +
                                                                 "<Filter>" + directories.Name + "</Filter>\n" +
                                                                 "</ClCompile>\n" +
                                                                 "</ItemGroup>\n";
                                            }
                                            else if (files.Extension.ToLower() == ".r" || files.Extension.ToLower() == ".rc")
                                            {
                                                resultFilters += "<ItemGroup>\n" +
                                                                 "<ClInclude Include=\"" + files.FullName.Replace(destinationDir, "") + "\">\n" +
                                                                 "<Filter>" + directories.Name + "</Filter>\n" +
                                                                 "</ClInclude>\n" +
                                                                 "</ItemGroup>\n";
                                            }
                                        }
                                    }
                                }
                            }
                            resultFilters += "</Project>\n";
                            result        += "</ItemGroup >\n" +
                                             "<Import Project = \"$(VCTargetsPath)\\Microsoft.Cpp.targets\"/>\n" +
                                             "<ImportGroup Label = \"ExtensionTargets\" >\n" +
                                             "</ImportGroup>\n" +
                                             "</Project>\n";

                            string projectFile = destinationDir + originalFileInfo.Name.Replace(originalFileInfo.Extension, "") + ".vcxproj";
                            string filterFile  = destinationDir + originalFileInfo.Name.Replace(originalFileInfo.Extension, "") + ".vcxproj.filters";

                            File.WriteAllText(projectFile, result);
                            File.WriteAllText(filterFile, resultFilters);
                            UpdateStatusBar(originalFileInfo.Name.Replace(originalFileInfo.Extension, "") + " successfully upgraded to Visual Studio Tools format.");
                            Dte.Solution.AddFromFile(projectFile);
                        }
                        else
                        {
                            UpdateStatusBar(originalFileInfo.Name.Replace(originalFileInfo.Extension, "") + " project creation failed: Project already exist in Visual Studio projects folder.");
                        }
                    }
                }
            }
        }