Esempio n. 1
0
        void CreateTempAssemblyInfo(VersionVariables versionVariables)
        {
            var assemblyInfoBuilder = AssemblyInfoBuilder.GetAssemblyInfoBuilder(CompileFiles);

            if (IntermediateOutputPath == null)
            {
                var tempFileName = string.Format("AssemblyInfo_{0}_{1}.g.{2}", Path.GetFileNameWithoutExtension(ProjectFile), Path.GetRandomFileName(), assemblyInfoBuilder.AssemblyInfoExtension);
                AssemblyInfoTempFilePath = Path.Combine(TempFileTracker.TempPath, tempFileName);
            }
            else
            {
                AssemblyInfoTempFilePath = Path.Combine(IntermediateOutputPath, string.Format("GitVersionTaskAssemblyInfo.g.{0}", assemblyInfoBuilder.AssemblyInfoExtension));
            }

            var assemblyInfo = assemblyInfoBuilder.GetAssemblyInfoText(versionVariables, RootNamespace).Trim();
            var encoding     = EncodingHelper.DetectEncoding(AssemblyInfoTempFilePath) ?? Encoding.UTF8;

            // We need to try to read the existing text first if the file exists and see if it's the same
            // This is to avoid writing when there's no differences and causing a rebuild
            try
            {
                if (File.Exists(AssemblyInfoTempFilePath))
                {
                    var content = File.ReadAllText(AssemblyInfoTempFilePath, encoding).Trim();
                    if (string.Equals(assemblyInfo, content, StringComparison.Ordinal))
                    {
                        return; // nothign to do as the file matches what we'd create
                    }
                }
            }
            catch (Exception)
            {
                // Something happened reading the file, try to overwrite anyway
            }

            File.WriteAllText(AssemblyInfoTempFilePath, assemblyInfo, encoding);
        }