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

README - compilable code snippets #6609

Merged
merged 6 commits into from
Dec 3, 2019
Merged

Conversation

srnagar
Copy link
Member

@srnagar srnagar commented Dec 2, 2019

READMEs are the first point of interaction for users looking to use our SDKs. It contains several useful code snippets for users to get started quickly. It is important to keep these code samples correct at all times. However, when code is modified README gets outdated and hard to figure out which README is referencing the modified piece of code.

So, this change will introduce a mechanism to inject code from a compiled Java class to ensure freshness of samples.

Tool used - https://github.com/zakhenry/embedme

How to use:

  1. Install NodeJS if you don't already have it.
  2. In README files, use the following language tag for all code snippets that you want to provide:
```java
```
  1. Within the tag, add a comment (should be the only line) providing the path (relative to the README file) to the Java file containing the code snippet that you want to replace with.
// path/to/class/containing/Samples.java
  1. A sub-section of the Java file can also be used by providing line numbers.
// path/to/class/containing/Samples.java#L20-L30
  1. Next run the embedme tool as shown below:
npx embedme path/to/README.md
  1. The code should now be inserted into the README file from the java class.
  2. Verify the changes to the README file.

This PR is using this tool for

  • Core libraries
  • Identity

Partially fixes #6608

@JonathanGiles
Copy link
Member

I like this a lot. It will help to ensure our samples are always accurate and compiling. I think introducing a new sample file (ReadmeSamples.java) is a good idea and should be used consistently, especially given the line referencing approach used in the markdown. I would recommend you introduce some warning text at the top of these files to warn users against making any changes to the code that may result in line number differences.

@alzimmermsft
Copy link
Member

Would it be possible to update the Blob Batch README to use these? It already has a ReadmeCodeSamples class.

@alzimmermsft
Copy link
Member

@@ -34,6 +34,8 @@ The following sections provide several code snippets covering some of the most c
Create a Netty Http client that uses port 80 and has no proxy.

```java
// ./src/samples/java/com/azure/core/http/netty/ReadmeSamples.java#L19-L19
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to hide this in the README?

Copy link
Member Author

Choose a reason for hiding this comment

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

There is a way to hide but I like to leave it as it is so that we'll know where the snippets are coming from and leaving it here will allow for future updates to automatically pick up the changes.

@srnagar
Copy link
Member Author

srnagar commented Dec 2, 2019

@JonathanGiles Added a warning message to ReadmeSamples.java to ensure line numbers are correct.
@alzimmermsft - Updated storage blob batch README to use the code snippets in java file.

@@ -558,7 +558,8 @@
<Match>
<Or>
<Class name="~.*JavaDoc(CodeSnippets|CodeSamples|Samples)"/>
<Class name="com.azure.storage.blob.batch.ReadmeCodeSamples"/>
<Class name="~.*ReadmeSamples"/>
<Class name="com.azure.storage.blob.batch.ReadmeSamples"/>
Copy link
Member

Choose a reason for hiding this comment

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

You shouldn't need to explicitly call out the batch file should you?

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed it.

@@ -34,6 +34,8 @@ The following sections provide several code snippets covering some of the most c
Create a Netty Http client that uses port 80 and has no proxy.

```java
// ./src/samples/java/com/azure/core/http/netty/ReadmeSamples.java#L23-L23
Copy link
Member

Choose a reason for hiding this comment

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

I agree with Alan, I'm not convinced that we want these to show up in all of the code samples. If we can hide it, whilst still retaining the ability for it to be updated when the sample code is changed, that would be best IMHO.

Copy link
Member Author

Choose a reason for hiding this comment

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

Using alternate embedding syntax that will hide the comment in README and will continue to be updated when sample code changes.

@JonathanGiles
Copy link
Member

Should we introduce tooling into /eng that would run this over all readme files?

@srnagar
Copy link
Member Author

srnagar commented Dec 2, 2019

@JonathanGiles Yes, I will have a follow-up task to introduce tooling that will automatically run this to update READMEs. This PR is a first step to get everyone's thoughts on the tool and usage. If we want to go ahead with this, I will look into integrating with eng tools.

@srnagar srnagar requested a review from samvaity as a code owner December 3, 2019 20:43
@srnagar srnagar merged commit 7fad541 into Azure:master Dec 3, 2019
xseeseesee pushed a commit that referenced this pull request Dec 10, 2019
* Use compilable code snippets in README

* Update paths and line numbers

* Add warning to ReadmeSamples.java and update storage blob batch snippets as well

* Address PR comments

* Update Identity samples

* Remove unused file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compilable code snippets in README files
3 participants