Beispiel #1
0
        private RootedPath(string root)
        {
            if (System.IO.Directory.Exists(root))
            {
                Root = new System.IO.DirectoryInfo(root).FullName;
                Sep  = Root.Contains('/') ? '/' :
                       Root.Contains('\\') ? '\\' : throw new Exception("UnKnown Seperator");
                AltSep = Sep == '/' ? '\\' : '/';
                return;
            }

            throw new Exception($"Could not find any root path '{Root}'. Curr: '{Environment.CurrentDirectory}'");
        }
Beispiel #2
0
        static int SyncDirs(string sourceDir, string destDir, bool onlyLog = true)
        {
            int totalDirs = 0;
            if (!Directory.Exists(sourceDir))
            {
                Console.WriteLine("Dir not exist: " + sourceDir);
                return totalDirs;
            }
            if (!Directory.Exists(destDir))
            {
                Console.WriteLine("Dir not exist: " + destDir);
                return totalDirs;
            }

            var destDirs = new DirectoryInfo(destDir).GetDirectories().Select(d => d.Name).ToList();
            foreach(var sourceDirName in (new DirectoryInfo(sourceDir).GetDirectories()))
            {
                if (!destDirs.Contains(sourceDirName.Name))
                {
                    totalDirs++;
                    string newDir = Path.Combine(destDir, sourceDirName.Name);
                    if (onlyLog)
                    {
                        Console.WriteLine("Create dir {0}?", newDir);
                    }else
                    {
                        Console.WriteLine("Create dir {0}", newDir);
                        Directory.CreateDirectory(newDir);
                    }
                }
            }
            return totalDirs;
        }
        /// <summary>
        /// This is required for debugging as NHibernate needs to load the DLLs and seems to only look in the current folder.
        /// When run live it will be run via powershell which will put the EXE in the destination folder.
        /// Debugging will have to be run through here, NHMigrate will not work correctly.
        /// </summary>
        /// <param name="projectFolder"></param>
        private void CopyDllsForDomainLoadingDebug(string projectFolder)
        {
            var projectDirectory = new DirectoryInfo(projectFolder);

            var executingBinFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6);

            var currentFilesInDestination = new DirectoryInfo(executingBinFolder).GetFiles("*.dll").Select(f => f.Name).ToList();
            var dlls = projectDirectory.GetFiles("*.dll");

            foreach (var fileInfo in dlls)
            {
                var destFileName = Path.Combine(executingBinFolder, fileInfo.Name);

                if (currentFilesInDestination.Contains(fileInfo.Name))
                {
                    try
                    {
                        File.Delete(destFileName);
                    }
                    catch (Exception)
                    {
                        //try and delete the old files, ignore errors
                    }
                };

                File.Copy(fileInfo.FullName, destFileName);
            }
        }
 /// <summary>
 /// Determines if the file or directory is contained within the the other directory at any depth.
 /// </summary>
 public static bool IsContainedWithin(this FileSystemInfo fileOrDirectory, DirectoryInfo otherDir)
 {
     return otherDir.Contains(fileOrDirectory);
 }
        public ReleasePackage ApplyDeltaPackage(ReleasePackage basePackage, ReleasePackage deltaPackage, string outputFile)
        {
            Contract.Requires(deltaPackage != null);
            Contract.Requires(!String.IsNullOrEmpty(outputFile) && !File.Exists(outputFile));

            string workingPath;
            string deltaPath;

            using (Utility.WithTempDirectory(out deltaPath, localAppDirectory))
            using (Utility.WithTempDirectory(out workingPath, localAppDirectory)) {
                var fz = new FastZip();
                fz.ExtractZip(deltaPackage.InputPackageFile, deltaPath, null);
                fz.ExtractZip(basePackage.InputPackageFile, workingPath, null);

                var pathsVisited = new List<string>();

                var deltaPathRelativePaths = new DirectoryInfo(deltaPath).GetAllFilesRecursively()
                    .Select(x => x.FullName.Replace(deltaPath + Path.DirectorySeparatorChar, ""))
                    .ToArray();

                // Apply all of the .diff files
                deltaPathRelativePaths
                    .Where(x => x.StartsWith("lib", StringComparison.InvariantCultureIgnoreCase))
                    .Where(x => !x.EndsWith(".shasum", StringComparison.InvariantCultureIgnoreCase))
                    .Where(x => !x.EndsWith(".diff", StringComparison.InvariantCultureIgnoreCase) ||
                                !deltaPathRelativePaths.Contains(x.Replace(".diff", ".bsdiff")))
                    .ForEach(file => {
                        pathsVisited.Add(Regex.Replace(file, @"\.(bs)?diff$", "").ToLowerInvariant());
                        applyDiffToFile(deltaPath, file, workingPath);
                    });

                // Delete all of the files that were in the old package but
                // not in the new one.
                new DirectoryInfo(workingPath).GetAllFilesRecursively()
                    .Select(x => x.FullName.Replace(workingPath + Path.DirectorySeparatorChar, "").ToLowerInvariant())
                    .Where(x => x.StartsWith("lib", StringComparison.InvariantCultureIgnoreCase) && !pathsVisited.Contains(x))
                    .ForEach(x => {
                        this.Log().Info("{0} was in old package but not in new one, deleting", x);
                        File.Delete(Path.Combine(workingPath, x));
                    });

                // Update all the files that aren't in 'lib' with the delta
                // package's versions (i.e. the nuspec file, etc etc).
                deltaPathRelativePaths
                    .Where(x => !x.StartsWith("lib", StringComparison.InvariantCultureIgnoreCase))
                    .ForEach(x => {
                        this.Log().Info("Updating metadata file: {0}", x);
                        File.Copy(Path.Combine(deltaPath, x), Path.Combine(workingPath, x), true);
                    });

                this.Log().Info("Repacking into full package: {0}", outputFile);
                fz.CreateZip(outputFile, workingPath, true, null);
            }

            return new ReleasePackage(outputFile);
        }
        /// <summary>
        /// Cleans up a package after it has been installed.
        /// Since we are in Unity, we can make certain assumptions on which files will NOT be used, so we can delete them.
        /// </summary>
        /// <param name="package">The NugetPackage to clean.</param>
        private static void Clean(NugetPackageIdentifier package)
        {
            string packageInstallDirectory = Path.Combine(NugetConfigFile.RepositoryPath, string.Format("{0}.{1}", package.Id, package.Version));

            LogVerbose("Cleaning {0}", packageInstallDirectory);

            FixSpaces(packageInstallDirectory);

            // delete a remnant .meta file that may exist from packages created by Unity
            DeleteFile(packageInstallDirectory + "/" + package.Id + ".nuspec.meta");

            // delete directories & files that NuGet normally deletes, but since we are installing "manually" they exist
            DeleteDirectory(packageInstallDirectory + "/_rels");
            DeleteDirectory(packageInstallDirectory + "/package");
            DeleteFile(packageInstallDirectory + "/" + package.Id + ".nuspec");
            DeleteFile(packageInstallDirectory + "/[Content_Types].xml");

            // Unity has no use for the build directory
            DeleteDirectory(packageInstallDirectory + "/build");

            // For now, delete Content.  We may use it later...
            DeleteDirectory(packageInstallDirectory + "/Content");

            // For now, delete src.  We may use it later...
            DeleteDirectory(packageInstallDirectory + "/src");

            // Delete documentation folders since they sometimes have HTML docs with JavaScript, which Unity tried to parse as "UnityScript"
            DeleteDirectory(packageInstallDirectory + "/docs");

            // Unity can only use .NET 3.5 or lower, so delete everything else
            if (Directory.Exists(packageInstallDirectory + "/lib"))
            {
                //bool has20 = Directory.Exists(packageInstallDirectory + "/lib/net20");
                bool has30 = Directory.Exists(packageInstallDirectory + "/lib/net30");
                bool has35 = Directory.Exists(packageInstallDirectory + "/lib/net35");

                // See here: https://docs.nuget.org/ndocs/schema/target-frameworks
                List<string> directoriesToDelete = new List<string>
                {
                    // .NET Framework
                    "net4",
                    "net40",
                    "net403",
                    "net45",
                    "net451",
                    "net452",
                    "net46",
                    "net461",
                    "net462",
                    // .NET Core
                    "netcore",
                    "netcore45",
                    "netcore451",
                    "netcore50",
                    // .NET MicroFramework
                    "netmf",
                    // Windows
                    "win", // windows store
                    "win8",
                    "win81",
                    "win10",
                    // Silverlight
                    "sl",
                    "sl4",
                    "sl5",
                    // Windows Phone
                    "windowsphone",
                    "wp",
                    "wp7",
                    "wp75",
                    "wp8",
                    "wp81",
                    "wpa81",
                    // Universal Windows Platform
                    "uap",
                    "uap10",
                    // .NET Standard
                    // See here: https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/
                    "netstandard",
                    "netstandard1.0",
                    "netstandard1.1",
                    "netstandard1.2",
                    "netstandard1.3",
                    "netstandard1.4",
                    "netstandard1.5",
                    "netstandard1.6",
                    // .NET Core App
                    "netcoreapp",
                    "netcoreapp1.0",
                    "cf", // compact framework
                    "monoandroid",
                    "monotouch",
                    "xamarin.ios10",
                    "xamarin.mac20"
                };

                string[] libDirectories = Directory.GetDirectories(packageInstallDirectory + "/lib");
                foreach (var directory in libDirectories)
                {
                    string directoryName = new DirectoryInfo(directory).Name.ToLower();
                    if (directoriesToDelete.Any(directoryName.Contains))
                    {
                        LogVerbose("Deleting unused directory: {0}", directory);
                        DeleteDirectory(directory);
                    }
                    else if (directoryName.Contains("net20") && (has30 || has35))
                    {
                        // if .NET 2.0 exists, keep it, unless there is also a .NET 3.0 or 3.5 version as well
                        LogVerbose("Deleting net20: {0}", directory);
                        DeleteDirectory(directory);
                    }
                    else if (directoryName.Contains("net30") && has35)
                    {
                        // if .NET 3.0 exists, keep it, unless there is also a .NET 3.5 version as well
                        LogVerbose("Deleting net30: {0}", directory);
                        DeleteDirectory(directory);
                    }
                }
            }

            if (Directory.Exists(packageInstallDirectory + "/tools"))
            {
                // Delete all JavaScript in the tools directory since Unity will think it's "UnityScript"
                DeleteAllFiles(packageInstallDirectory + "/tools", "*.js");

                // Delete all DLLs in the tools directory since we can't tell which .NET version they target
                DeleteAllFiles(packageInstallDirectory + "/tools", "*.dll");
            }

            // delete all PDB files since Unity uses Mono and requires MDB files, which causes it to output "missing MDB" errors
            DeleteAllFiles(packageInstallDirectory, "*.pdb");

            // if there are native DLLs, copy them to the Unity project root (1 up from Assets)
            if (Directory.Exists(packageInstallDirectory + "/output"))
            {
                string[] files = Directory.GetFiles(packageInstallDirectory + "/output");
                foreach (string file in files)
                {
                    string newFilePath = Directory.GetCurrentDirectory() + "/" + Path.GetFileName(file);
                    LogVerbose("Moving {0} to {1}", file, newFilePath);
                    DeleteFile(newFilePath);
                    File.Move(file, newFilePath);
                }

                LogVerbose("Deleting {0}", packageInstallDirectory + "/output");

                DeleteDirectory(packageInstallDirectory + "/output");
            }

            // if there are Unity plugin DLLs, copy them to the Unity Plugins folder (Assets/Plugins)
            if (Directory.Exists(packageInstallDirectory + "/unityplugin"))
            {
                string pluginsDirectory = Application.dataPath + "/Plugins/";

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

                string[] files = Directory.GetFiles(packageInstallDirectory + "/unityplugin");
                foreach (string file in files)
                {
                    string newFilePath = pluginsDirectory + Path.GetFileName(file);

                    try
                    {
                        LogVerbose("Moving {0} to {1}", file, newFilePath);
                        DeleteFile(newFilePath);
                        File.Move(file, newFilePath);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        Debug.LogWarningFormat("{0} couldn't be overwritten. It may be a native plugin already locked by Unity. Please close Unity and manually delete it.", newFilePath);
                    }
                }

                LogVerbose("Deleting {0}", packageInstallDirectory + "/unityplugin");

                DeleteDirectory(packageInstallDirectory + "/unityplugin");
            }

            // if there are Unity StreamingAssets, copy them to the Unity StreamingAssets folder (Assets/StreamingAssets)
            if (Directory.Exists(packageInstallDirectory + "/StreamingAssets"))
            {
                string streamingAssetsDirectory = Application.dataPath + "/StreamingAssets/";

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

                // move the files
                string[] files = Directory.GetFiles(packageInstallDirectory + "/StreamingAssets");
                foreach (string file in files)
                {
                    string newFilePath = streamingAssetsDirectory + Path.GetFileName(file);

                    try
                    {
                        LogVerbose("Moving {0} to {1}", file, newFilePath);
                        DeleteFile(newFilePath);
                        File.Move(file, newFilePath);
                    }
                    catch (Exception e)
                    {
                        Debug.LogWarningFormat("{0} couldn't be moved. \n{1}", newFilePath, e.ToString());
                    }
                }

                // move the directories
                string[] directories = Directory.GetDirectories(packageInstallDirectory + "/StreamingAssets");
                foreach (string directory in directories)
                {
                    string newDirectoryPath = streamingAssetsDirectory + new DirectoryInfo(directory).Name;

                    try
                    {
                        LogVerbose("Moving {0} to {1}", directory, newDirectoryPath);
                        if (Directory.Exists(newDirectoryPath))
                        {
                            DeleteDirectory(newDirectoryPath);
                        }
                        Directory.Move(directory, newDirectoryPath);
                    }
                    catch (Exception e)
                    {
                        Debug.LogWarningFormat("{0} couldn't be moved. \n{1}", newDirectoryPath, e.ToString());
                    }
                }

                // delete the package's StreamingAssets folder and .meta file
                LogVerbose("Deleting {0}", packageInstallDirectory + "/StreamingAssets");
                DeleteDirectory(packageInstallDirectory + "/StreamingAssets");
                DeleteFile(packageInstallDirectory + "/StreamingAssets.meta");
            }
        }