Esempio n. 1
0
def generate(
    sdk_root: str,
    service: str,
    spec_root: str,
    readme: str,
    autorest: str,
    use: str,
    output_folder: str,
    module: str,
    namespace: str,
    tag: str = None,
    version: str = None,
    autorest_options: str = '',
    **kwargs,
) -> bool:
    output_dir = os.path.join(
        sdk_root,
        'sdk/{0}'.format(service),
        module,
    )
    shutil.rmtree(os.path.join(output_dir, 'src/main'), ignore_errors=True)
    if os.path.exists(os.path.join(output_dir, 'src/samples/README.md')):
        # samples contains hand-written code
        shutil.rmtree(os.path.join(output_dir, 'src/samples/java',
                                   namespace.replace('.', '/'), 'generated'),
                      ignore_errors=True)
    else:
        shutil.rmtree(os.path.join(output_dir, 'src/samples'),
                      ignore_errors=True)

    if re.match(r'https?://', spec_root):
        readme = urllib.parse.urljoin(spec_root, readme)
    else:
        readme = os.path.join(spec_root, readme)

    tag_option = '--tag={0}'.format(tag) if tag else ''
    version_option = '--package-version={0}'.format(version) if version else ''

    command = 'autorest --version={0} --use={1} --java --java.java-sdks-folder={2} --java.output-folder={3} ' \
              '--java.namespace={4} {5}'\
        .format(
            autorest,
            use,
            os.path.abspath(sdk_root),
            os.path.abspath(output_dir),
            namespace,
            ' '.join((tag_option, version_option, FLUENTLITE_ARGUMENTS, autorest_options, readme)),
        )
    logging.info(command)
    if os.system(command) != 0:
        logging.error('[GENERATE] Autorest fail')
        return False

    group = GROUP_ID
    update_service_ci_and_pom(sdk_root, service, group, module)
    update_root_pom(sdk_root, service)
    update_version(sdk_root, output_folder)

    return True
def generate(
    sdk_root: str,
    input_file: str,
    service: str,
    module: str,
    credential_types: str,
    credential_scopes: str,
    title: str,
    autorest: str,
    use: str,
    autorest_options: str = '',
    **kwargs,
) -> bool:
    namespace = 'com.{0}'.format(module.replace('-', '.'))
    output_dir = os.path.join(sdk_root, 'sdk', service, module)
    shutil.rmtree(os.path.join(output_dir, 'src/main'), ignore_errors=True)
    shutil.rmtree(os.path.join(output_dir, 'src/samples/java',
                               namespace.replace('.', '/'), 'generated'),
                  ignore_errors=True)

    readme_relative_path = update_readme(output_dir, input_file,
                                         credential_types, credential_scopes,
                                         title)
    if readme_relative_path:
        logging.info(
            '[GENERATE] Autorest from README {}'.format(readme_relative_path))

        command = 'autorest --version={0} --use={1} {2}'.format(
            autorest, use, readme_relative_path)
        logging.info(command)
        try:
            subprocess.run(command, shell=True, cwd=output_dir, check=True)
        except subprocess.CalledProcessError:
            logging.error('[GENERATE] Autorest fail')
            return False
    else:
        logging.info('[GENERATE] Autorest from JSON {}'.format(input_file))

        credential_arguments = '--java.credential-types={0}'.format(
            credential_types)
        if credential_scopes:
            credential_arguments += ' --java.credential-scopes={0}'.format(
                credential_scopes)

        input_arguments = '--input-file={0}'.format(input_file)

        artifact_arguments = '--artifact-id={0}'.format(module)
        if title:
            artifact_arguments += ' --title={0}'.format(title)

        command = 'autorest --version={0} --use={1} ' \
                  '--java.azure-libraries-for-java-folder={2} --java.output-folder={3} ' \
                  '--java.namespace={4} {5}'\
            .format(
                autorest,
                use,
                os.path.abspath(sdk_root),
                os.path.abspath(output_dir),
                namespace,
                ' '.join((LLC_ARGUMENTS, input_arguments, credential_arguments, artifact_arguments, autorest_options))
            )
        logging.info(command)
        if os.system(command) != 0:
            logging.error('[GENERATE] Autorest fail')
            return False

        set_or_default_version(sdk_root, GROUP_ID, module)
        update_service_ci_and_pom(sdk_root, service, GROUP_ID, module)
        update_root_pom(sdk_root, service)
        # skip version update script, as current automation does not support automatic version increment
        # update_version(sdk_root, output_dir)

    return True
def generate(
    sdk_root: str,
    input_file: str,
    service: str,
    module: str,
    security: str,
    security_scopes: str,
    title: str,
    autorest: str,
    use: str,
    autorest_options: str = '',
    readme: str = None,
    **kwargs,
) -> bool:
    namespace = 'com.{0}'.format(module.replace('-', '.'))
    output_dir = os.path.join(sdk_root, 'sdk', service, module)
    # shutil.rmtree(os.path.join(output_dir, 'src/main'), ignore_errors=True)
    shutil.rmtree(os.path.join(output_dir, 'src/samples/java',
                               namespace.replace('.', '/'), 'generated'),
                  ignore_errors=True)
    shutil.rmtree(os.path.join(output_dir, 'src/tests/java',
                               namespace.replace('.', '/'), 'generated'),
                  ignore_errors=True)

    if readme:
        # use readme from spec repo
        readme_file_path = readme

        require_sdk_integration = not os.path.exists(
            os.path.join(output_dir, 'src'))

        logging.info(
            '[GENERATE] Autorest from README {}'.format(readme_file_path))

        command = 'autorest --version={0} --use={1} --java --java.java-sdks-folder={2} --java.output-folder={3} {4} {5}'\
            .format(
                autorest,
                use,
                os.path.abspath(sdk_root),
                os.path.abspath(output_dir),
                readme_file_path,
                autorest_options
            )
        if require_sdk_integration:
            command += ' --java.namespace={0} '.format(
                namespace) + LLC_ARGUMENTS
        logging.info(command)
        try:
            subprocess.run(command, shell=True, check=True)
        except subprocess.CalledProcessError:
            logging.error('[GENERATE] Autorest fail')
            return False

        if require_sdk_integration:
            set_or_default_version(sdk_root, GROUP_ID, module)
            update_service_ci_and_pom(sdk_root, service, GROUP_ID, module)
            update_root_pom(sdk_root, service)
    else:
        readme_file_path = update_readme(output_dir, input_file, security,
                                         security_scopes, title)
        if readme_file_path:
            # use readme from SDK repo

            logging.info(
                '[GENERATE] Autorest from README {}'.format(readme_file_path))

            command = 'autorest --version={0} --use={1} --java --java.java-sdks-folder={2} ' \
                      '--java.output-folder={2} {3} {4}'\
                .format(
                    autorest,
                    use,
                    os.path.abspath(sdk_root),
                    os.path.abspath(output_dir),
                    readme_file_path,
                    autorest_options
                )
            logging.info(command)
            try:
                subprocess.run(command, shell=True, cwd=output_dir, check=True)
            except subprocess.CalledProcessError:
                logging.error('[GENERATE] Autorest fail')
                return False
        else:
            # no readme

            logging.info('[GENERATE] Autorest from JSON {}'.format(input_file))

            security_arguments = ''
            if security:
                security_arguments += '--security={0}'.format(security)
            if security_scopes:
                security_arguments += ' --security-scopes={0}'.format(
                    security_scopes)

            input_arguments = '--input-file={0}'.format(input_file)

            artifact_arguments = '--artifact-id={0}'.format(module)
            if title:
                artifact_arguments += ' --title={0}'.format(title)

            command = 'autorest --version={0} --use={1} --java ' \
                      '--java.java-sdks-folder={2} --java.output-folder={3} ' \
                      '--java.namespace={4} {5}'\
                .format(
                    autorest,
                    use,
                    os.path.abspath(sdk_root),
                    os.path.abspath(output_dir),
                    namespace,
                    ' '.join((LLC_ARGUMENTS, input_arguments, security_arguments, artifact_arguments, autorest_options))
                )
            logging.info(command)
            if os.system(command) != 0:
                logging.error('[GENERATE] Autorest fail')
                return False

            set_or_default_version(sdk_root, GROUP_ID, module)
            update_service_ci_and_pom(sdk_root, service, GROUP_ID, module)
            update_root_pom(sdk_root, service)
            # update_version(sdk_root, output_dir)

    return True