Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify README embedme Usage into a Wrapper Script #21859

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions eng/scripts/invoke_embedme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

# Python version 3.4 or higher is required to run this script.

# Use case: Invokes embedme README codesnippet generation and validation regardless of OS.
#
# Flags
# --readme/-r: Path to the README.
# --verify/-v: Flag indicating to only perform a dry-run validation.
# --debug/-d: Flag indicating to perform debug level logging.
#
# For example: Generating README codesnippets for Azure Storage Blobs.
# python eng/scripts/invoke_embedme.py -r sdk/storage/azure-storage-blob/README.md
#
# For example: Valdate README codesnippets for Azure Core.
# python eng/scripts/invoke_embedme.py -r sdk/core/azure-core/README.md -v
#
# The script must be run at the root of azure-sdk-for-java.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. You can easily check this on run-time and throw early instead of the undefined-behavior. But not such a big deal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just switch to that directory based on the path of this script.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a standard comment in all the Python scripts used as tooling, I don't think any check the path used either.


import argparse
import os
import subprocess
import sys

# From this file get to the root path of the repo.
root_path = os.path.normpath(os.path.abspath(__file__) + '/../../../')
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved

# NPX command for Windows OS.
windows_command = 'npx.cmd'
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved

# NPX command for Posix OSes.
posix_command = 'npx'

# Invoke embedme.
def invoke_embedme(readme: str, verify: bool, debug: bool):
command = ''
if os.name == 'nt':
command = windows_command
else:
command = posix_command

command += ' embedme'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you also need to install this command?

I must say it is a little odd that we are using maven to call python to call npm lol :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I don't disagree on this oddity of Maven -> Python -> npm. Hopefully as our own tooling mature further these will be simplified into Maven plugins so that everything at least builds into Maven directly.


# If the passed README path was relative
command += ' ' + os.path.abspath(readme)


if verify:
command += ' --verify'

if debug:
print('Running embedme command: {}'.format(command))

sys.exit(os.system(command))

def main():
parser = argparse.ArgumentParser(description='Invokes embedme README codesnippet generation and validation regardless of OS.')
parser.add_argument('--readme', '-r', type=str, required=True, help='Path to the README')
parser.add_argument('--verify', '-v', action='store_true', help='Flag indicating to only perform a dry-run validation')
parser.add_argument('--debug', '-d', action='store_true', help='Flag indicating to perform debug level logging')
args = parser.parse_args()

invoke_embedme(args.readme, args.verify, args.debug)

if __name__ == '__main__':
main()
86 changes: 8 additions & 78 deletions sdk/parents/azure-client-sdk-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -947,41 +947,6 @@
</build>
</profile>

<profile>
<!-- separate profile for windows as the executable on windows is named npx.cmd and npx on other os families -->
<id>readme-codesnippet-windows</id>
<activation>
<property>
<name>readme-codesnippet-windows</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version> <!-- {x-version-update;org.codehaus.mojo:exec-maven-plugin;external_dependency} -->
<executions>
<execution>
<id>code-snippet-for-readme-windows</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npx.cmd</executable>
<arguments>
<argument>embedme</argument>
<argument>${project.basedir}/README.md</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

<!-- Profiles for updating README file using embedme tool to inject code snippets from compilable Java source files.
Activate the profile below to update README files when corresponding Java files are updated. -->
<profile>
Expand All @@ -1005,45 +970,10 @@
<goal>exec</goal>
</goals>
<configuration>
<executable>npx</executable>
<arguments>
<argument>embedme</argument>
<argument>${project.basedir}/README.md</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

<profile>
<!-- separate profile for windows as the executable on windows is named npx.cmd and npx on other os families -->
<id>verify-readme-windows</id>
<activation>
<property>
<name>verify-readme-windows</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version> <!-- {x-version-update;org.codehaus.mojo:exec-maven-plugin;external_dependency} -->
<executions>
<execution>
<id>verify-readme-codesnippet-windows</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npx.cmd</executable>
<executable>python</executable>
<arguments>
<argument>embedme</argument>
<argument>--verify</argument>
<argument>../../../eng/scripts/invoke_embedme.py</argument>
<argument>--readme</argument>
<argument>${project.basedir}/README.md</argument>
</arguments>
</configuration>
Expand All @@ -1054,8 +984,7 @@
</build>
</profile>

<!-- Verify that there are no changes to readme file. Build fails if the there's a difference in README
after running the embedme tool. -->
<!-- Verify that there are no changes to readme file. Build fails if the there's a difference in README after running the embedme tool. -->
<profile>
<id>verify-readme</id>
<activation>
Expand All @@ -1077,11 +1006,12 @@
<goal>exec</goal>
</goals>
<configuration>
<executable>npx</executable>
<executable>python</executable>
<arguments>
<argument>embedme</argument>
<argument>--verify</argument>
<argument>../../../eng/scripts/invoke_embedme.py</argument>
<argument>--readme</argument>
<argument>${project.basedir}/README.md</argument>
<argument>--verify</argument>
</arguments>
</configuration>
</execution>
Expand Down