Esempio n. 1
0
    def _locate_nuget_path(self, nuget_path):
        """
        Locates NuGet executable path from user input or NuGet facade.
        @:raises click.UsageError
        """
        # If NuGet path is provided, check if it's valid
        if nuget_path:
            if not NuGetRunner.valid_nuget_executable(nuget_path):
                raise click.UsageError(nuget_path + " is not a valid NuGet executable.")
            else:
                return nuget_path

        # NuGet is not provided, locate
        nuget_path = NuGetRunner.locate_nuget()
        if nuget_path:
            return nuget_path

        # Failed to locate
        if not self.quiet:
            if sys.platform == 'win32':
                click.secho('You can install NuGet from the official website: https://www.nuget.org/downloads')
                click.secho('You might need to add NuGet installation folder to your PATH variable.')
            elif sys.platform == 'darwin':
                click.secho('You can install NuGet using Homebrew ("brew install nuget") or '
                            'download from the official website')
                click.secho('You might need to add NuGet installation folder to your PATH variable.')
        raise click.UsageError('Failed to locate NuGet executable.')
Esempio n. 2
0
    def pack(self, path, output_dir, nuget_path, unitypackage_path,
             configuration, unity_project_path,
             unitypackage_root_path_relative):
        """
        Packs NuGet Package.
        :param path: Path to the .csproj or .nuspec, or a directory containing either
        :param output_dir: Output directory - this is where .nupkg file will be built
        :param nuget_path: Path to the NuGet executable
        :param unitypackage_path: Path to the .unitypackge
        :param configuration: Configuration - Debug/Release
        :return: Exit code of the NuGet Pack command
        """
        # Locate nuget executable
        nuget_path = self._locate_nuget_path(nuget_path)
        nuget_runner = NuGetRunner(nuget_path, self.debug)

        # Locate project name and version
        csproj_file_path = CsProj.get_csproj_at_path(path)
        if csproj_file_path is not None:
            csproj = CsProj(path, self.debug)
            package_id = csproj.get_assembly_name()
            version = csproj.get_assembly_version()
        else:
            nuspec_file_path = NuSpec.get_nuspec_at_path(path)
            if nuspec_file_path is not None:
                nuspec = NuSpec(path, self.debug)
                package_id = nuspec.get_package_id()
                version = nuspec.get_package_version()
            else:
                raise click.UsageError(
                    "Path must be a valid path to .nuspec, .csproj, or directory containing either"
                )

        if not package_id:
            raise click.UsageError("Failed to identify package id.")
        if not version:
            raise click.UsageError("Failed to identify package version.")

        if not unitypackage_path:
            unitypackage_name = utils.get_unitypackage_filename(
                package_id, version, configuration)
            unitypackage_path = os.path.join(output_dir, unitypackage_name)

        if not unitypackage_root_path_relative:
            unitypackage_root_path_relative = package_id

        unitypackage_export_root = self._get_unity_package_export_root(
            unity_project_path, unitypackage_root_path_relative)

        return nuget_runner.pack(path, output_dir, configuration,
                                 unitypackage_path, unitypackage_export_root)
Esempio n. 3
0
 def push(self, path, output_dir, feed, nuget_path, api_key):
     """
     Pushes NuGet package on to the NuGet feed.
     :param path: Path to the NuGet Package
     :param output_dir: Output directory in which NuGet package will be searched, if it's not explicitly provided.
     :param feed: NuGet feed URI
     :param nuget_path: Path to the NuGet executable
     :param api_key: NuGet Api Key
     :return: Exit code of the NuGet push command
     """
     nupkg_path = self._locate_nupkg_at_path(path, output_dir)
     nuget_path = self._locate_nuget_path(nuget_path)
     nuget = NuGetRunner(nuget_path, self.debug)
     return nuget.push(nupkg_path, feed, api_key)
Esempio n. 4
0
    def test_nuget_runner_push(self, mock_popen):
        """Test NuGetRunner.push"""
        mock_process = MagicMock()
        mock_process.wait.return_value = 0
        mock_popen.return_value = mock_process

        expected_command_str = "nuget.exe push Test.nupkg -Verbosity normal " \
                               "-Source http://test.com/nuget " \
                               "-ApiKey myapikey7"

        nuget_runner = NuGetRunner("nuget.exe")
        assert nuget_runner.push("Test.nupkg", "http://test.com/nuget",
                                 "myapikey7") == 0
        mock_popen.assert_called_with(expected_command_str, shell=True)
Esempio n. 5
0
    def test_nuget_runner_pack(self, mock_popen):
        """Test NuGetRunner.pack """
        mock_process = MagicMock()
        mock_process.wait.return_value = 0
        mock_popen.return_value = mock_process

        expected_command_str = "nuget.exe pack TestProject.csproj -OutputDirectory Output " \
                               "-Verbosity normal " \
                               "-Properties \"unityPackagePath=TestProject.1.0.0.unitypackage;unityPackageExportRoot=Some/Path;Configuration=Debug\"" \

        nuget_runner = NuGetRunner("nuget.exe")
        assert nuget_runner.pack("TestProject.csproj", "Output", "Debug",
                                 "TestProject.1.0.0.unitypackage",
                                 "Some/Path") == 0
        mock_popen.assert_called_with(expected_command_str, shell=True)
Esempio n. 6
0
    def _locate_nupkg_at_path(self, path, output_dir):
        """
        Finds .nupkg file.
        If no explicit path to the .nupkg file is provided, .csproj file will be searched for in path.
        .csproj will be used to determine name and version of the package, while package itself will be seached
        in the output_dir.
        """
        if path.endswith(".nupkg"):
            if not os.path.isfile(path):
                raise click.FileError(path)
            return path

        csproj_path = CsProj.get_csproj_at_path(path)

        if csproj_path:
            csproj = CsProj(csproj_path)

            assembly_name = csproj.get_assembly_name()
            version = csproj.get_assembly_version()
            normalized_version = NuGetRunner.get_normalized_nuget_pack_version(version)

            nupkg_filename = "{0}.{1}.nupkg".format(assembly_name, normalized_version)
            nupkg_path = os.path.normpath(os.path.join(output_dir, nupkg_filename))

            if not os.path.isfile(nupkg_path):
                raise click.UsageError("Failed to find Nuget Package (.nupkg) at path " + nupkg_path)

        else:
            nuspec_file_path = NuSpec.get_nuspec_at_path(path)
            if nuspec_file_path is not None:
                nuspec = NuSpec(path, self.debug)
                package_id = nuspec.get_package_id()
                version = nuspec.get_package_version()
                normalized_version = NuGetRunner.get_normalized_nuget_pack_version(version)

                nupkg_filename = "{0}.{1}.nupkg".format(package_id, normalized_version)
                nupkg_path = os.path.normpath(os.path.join(output_dir, nupkg_filename))
            else:
                raise click.UsageError("Path must be a valid path to .nuspec, .csproj, or directory containing either")

        return nupkg_path
Esempio n. 7
0
 def test_nuget_runner_valid_nuget_executable(self, mock_call, mock_open,
                                              mock_escape_exe_path):
     """Test NuGetRunner.valid_nuget_executable """
     mock_call.return_value = 0
     with open(os.devnull, "w") as devnull:
         mock_open_handler = MagicMock()
         mock_open.return_value = mock_open_handler
         mock_open_handler.__enter__.return_value = devnull
         mock_escape_exe_path.return_value = "nuget.exe"
         assert NuGetRunner.valid_nuget_executable("nuget.exe")
         mock_call.assert_called_with("nuget.exe help",
                                      shell=True,
                                      stderr=devnull,
                                      stdout=devnull)
Esempio n. 8
0
    def test_nuget_runner_get_normalized_nuget_pack_version(self):
        """Test NuGetRunner.get_normalized_nuget_pack_version """
        assert NuGetRunner.get_normalized_nuget_pack_version(
            "1.0.0") == "1.0.0"  # Does not change
        assert NuGetRunner.get_normalized_nuget_pack_version(
            "1.0.0.1") == "1.0.0.1"  # Does not change
        assert NuGetRunner.get_normalized_nuget_pack_version(
            "1.2.3.4") == "1.2.3.4"  # Does not change

        assert NuGetRunner.get_normalized_nuget_pack_version(
            "1.0.0.0") == "1.0.0"  # Drops last zero
        assert NuGetRunner.get_normalized_nuget_pack_version(
            "1.0.2.0") == "1.0.2"  # Drops last zero
        assert NuGetRunner.get_normalized_nuget_pack_version(
            "1.1.0.0") == "1.1.0"  # Drops last zero
        assert NuGetRunner.get_normalized_nuget_pack_version(
            "0.1.2.0") == "0.1.2"  # Drops last zero
Esempio n. 9
0
 def test_nuget_runner_locate_nuget(self, mock_call):
     """Test NuGetRunner.locate_nuget """
     mock_call.return_value = 0
     assert NuGetRunner.locate_nuget() == "nuget"