コード例 #1
0
        public override void ExecuteCommand()
        {
            var    manifest    = new CoreV2.NuGet.Manifest();
            string projectFile = null;
            string fileName    = null;

            if (!String.IsNullOrEmpty(AssemblyPath))
            {
                // Extract metadata from the assembly
                string           path     = Path.Combine(CurrentDirectory, AssemblyPath);
                AssemblyMetadata metadata = AssemblyMetadataExtractor.GetMetadata(path);
                manifest.Metadata.Id          = metadata.Name;
                manifest.Metadata.Version     = metadata.Version.ToString();
                manifest.Metadata.Authors     = metadata.Company;
                manifest.Metadata.Description = metadata.Description;
            }
            else
            {
                if (!ProjectHelper.TryGetProjectFile(CurrentDirectory, out projectFile))
                {
                    manifest.Metadata.Id      = Arguments.Any() ? Arguments[0] : "Package";
                    manifest.Metadata.Version = "1.0.0";
                }
                else
                {
                    fileName                      = Path.GetFileNameWithoutExtension(projectFile);
                    manifest.Metadata.Id          = "$id$";
                    manifest.Metadata.Title       = "$title$";
                    manifest.Metadata.Version     = "$version$";
                    manifest.Metadata.Description = "$description$";
                    manifest.Metadata.Authors     = "$author$";
                }
            }

            // Get the file name from the id or the project file
            fileName = fileName ?? manifest.Metadata.Id;

            // If we're using a project file then we want the a minimal nuspec
            if (String.IsNullOrEmpty(projectFile))
            {
                manifest.Metadata.Description = manifest.Metadata.Description ?? SampleDescription;
                if (String.IsNullOrEmpty(manifest.Metadata.Authors))
                {
                    manifest.Metadata.Authors = Environment.UserName;
                }
                manifest.Metadata.DependencySets = new List <CoreV2.NuGet.ManifestDependencySet>();
                manifest.Metadata.DependencySets.Add(new CoreV2.NuGet.ManifestDependencySet
                {
                    Dependencies = new List <CoreV2.NuGet.ManifestDependency> {
                        SampleManifestDependency
                    }
                });
            }

            manifest.Metadata.ProjectUrl   = SampleProjectUrl;
            manifest.Metadata.LicenseUrl   = SampleLicenseUrl;
            manifest.Metadata.IconUrl      = SampleIconUrl;
            manifest.Metadata.Tags         = SampleTags;
            manifest.Metadata.Copyright    = "Copyright " + DateTime.Now.Year;
            manifest.Metadata.ReleaseNotes = SampleReleaseNotes;
            string nuspecFile = fileName + CoreV2.NuGet.Constants.ManifestExtension;

            // Skip the creation if the file exists and force wasn't specified
            if (File.Exists(nuspecFile) && !Force)
            {
                Console.WriteLine(LocalizedResourceManager.GetString("SpecCommandFileExists"), nuspecFile);
            }
            else
            {
                try
                {
                    using (var stream = new MemoryStream())
                    {
                        manifest.Save(stream, validate: false);
                        stream.Seek(0, SeekOrigin.Begin);
                        string content = stream.ReadToEnd();
                        File.WriteAllText(nuspecFile, RemoveSchemaNamespace(content));
                    }

                    Console.WriteLine(LocalizedResourceManager.GetString("SpecCommandCreatedNuSpec"), nuspecFile);
                }
                catch
                {
                    // Cleanup the file if it fails to save for some reason
                    File.Delete(nuspecFile);
                    throw;
                }
            }
        }