Skip to content

Commit 6f70840

Browse files
update ClientFactory params in readme (Azure#24180)
1 parent cbe26d7 commit 6f70840

File tree

5 files changed

+119
-3
lines changed

5 files changed

+119
-3
lines changed

eng/scripts/automation_init.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ echo "Install generator..."
5252
go build 2>&1
5353

5454
cp generator $GOPATH/bin/
55+
rm generator
5556
export PATH=$GOPATH/bin:$PATH
5657
cd $DIRECTORY
5758

eng/tools/generator/cmd/v2/common/constants.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
package common
55

66
const (
7-
ChangelogFileName = "CHANGELOG.md"
8-
GoModFileName = "go.mod"
9-
SdkRootPath = "/sdk"
7+
ChangelogFileName = "CHANGELOG.md"
8+
GoModFileName = "go.mod"
9+
SdkRootPath = "/sdk"
10+
ReadmeFileName = "README.md"
11+
ClientFactoryFileName = "client_factory.go"
1012
)

eng/tools/generator/cmd/v2/common/fileProcessor.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,3 +837,57 @@ func FindModuleDirByGoMod(root string) (string, error) {
837837
}
838838
return "", fmt.Errorf("module not found, package path:%s", root)
839839
}
840+
841+
func UpdateReadmeClientFactory(path string) error {
842+
readmePath := filepath.Join(path, ReadmeFileName)
843+
readmeFile, err := os.ReadFile(readmePath)
844+
if err != nil {
845+
return err
846+
}
847+
noOptionalFactoryReg := regexp.MustCompile(`NewClientFactory\((.*?)(?:,\s*)?cred,\s*nil\)`)
848+
withOptionalFactoryReg := regexp.MustCompile(`NewClientFactory\((.*?)(?:,\s*)?cred,\s*&options\)`)
849+
oldnoOptionalFactory := noOptionalFactoryReg.FindString(string(readmeFile))
850+
oldwithOptionalFactory := withOptionalFactoryReg.FindString(string(readmeFile))
851+
if oldnoOptionalFactory == "" && oldwithOptionalFactory == "" {
852+
return nil
853+
}
854+
clientFactoryFile, err := os.ReadFile(filepath.Join(path, ClientFactoryFileName))
855+
if err != nil {
856+
return err
857+
}
858+
re := regexp.MustCompile(`func\s+NewClientFactory\(([^)]+)\)`)
859+
matches := re.FindStringSubmatch(string(clientFactoryFile))
860+
if len(matches) <= 1 {
861+
return nil
862+
}
863+
var factoryParams []string
864+
params := strings.Split(matches[1], ",")
865+
for param := range params {
866+
params[param] = strings.TrimSpace(params[param])
867+
paramDefinition := strings.Split(params[param], " ")
868+
if len(paramDefinition) != 2 {
869+
continue
870+
}
871+
paramName := paramDefinition[0]
872+
if paramName == "credential" || paramName == "options" {
873+
// fixed params, no need to process
874+
continue
875+
}
876+
if paramName == "subscriptionID" {
877+
// compatible with old version
878+
factoryParams = append(factoryParams, "<subscription ID>")
879+
continue
880+
}
881+
factoryParams = append(factoryParams, fmt.Sprintf("<%s>", paramName))
882+
}
883+
noOptionsParams := append(factoryParams, []string{"cred", "nil"}...)
884+
withOptionsParams := append(factoryParams, []string{"cred", "&options"}...)
885+
newNoOptionalFactory := fmt.Sprintf("NewClientFactory(%s)", strings.Join(noOptionsParams, ", "))
886+
newWithOptionalFactory := fmt.Sprintf("NewClientFactory(%s)", strings.Join(withOptionsParams, ", "))
887+
if oldnoOptionalFactory == newNoOptionalFactory && oldwithOptionalFactory == newWithOptionalFactory {
888+
return nil
889+
}
890+
content := strings.ReplaceAll(string(readmeFile), oldnoOptionalFactory, newNoOptionalFactory)
891+
content = strings.ReplaceAll(content, oldwithOptionalFactory, newWithOptionalFactory)
892+
return os.WriteFile(readmePath, []byte(content), 0644)
893+
}

eng/tools/generator/cmd/v2/common/fileProcessor_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,49 @@ func TestFindModule(t *testing.T) {
140140
assert.NoError(t, err)
141141
assert.Equal(t, "sdk/security/keyvault/azadmin", filepath.ToSlash(moduleRelativePath))
142142
}
143+
144+
func TestUpdateReadMeClientFactory(t *testing.T) {
145+
// Create temporary test directory
146+
tmpDir, err := os.MkdirTemp("", "tmp")
147+
assert.NoError(t, err)
148+
defer os.RemoveAll(tmpDir)
149+
150+
err = os.WriteFile(filepath.Join(tmpDir, "README.md"), []byte(`
151+
clientFactory, err := NewClientFactory(<subscription ID>, cred, nil)
152+
clientFactory, err := NewClientFactory(<subscription ID>, cred, &options)
153+
`), 0644)
154+
assert.NoError(t, err)
155+
156+
// Test case 1: without subscription ID
157+
err = os.WriteFile(filepath.Join(tmpDir, "client_factory.go"), []byte(`
158+
func NewClientFactory(credential azcore.TokenCredential, options *arm.ClientOptions) *ClientFactory {
159+
`), 0644)
160+
assert.NoError(t, err)
161+
162+
err = UpdateReadmeClientFactory(tmpDir)
163+
assert.NoError(t, err)
164+
165+
noSubContent, err := os.ReadFile(filepath.Join(tmpDir, "README.md"))
166+
assert.NoError(t, err)
167+
assert.Contains(t, string(noSubContent), "NewClientFactory(cred, nil)")
168+
assert.Contains(t, string(noSubContent), "NewClientFactory(cred, &options)")
169+
170+
// Test case 2: with subscription ID
171+
err = os.WriteFile(filepath.Join(tmpDir, "README.md"), []byte(`
172+
clientFactory, err := NewClientFactory(cred, nil)
173+
clientFactory, err := NewClientFactory(cred, &options)
174+
`), 0644)
175+
assert.NoError(t, err)
176+
err = os.WriteFile(filepath.Join(tmpDir, "client_factory.go"), []byte(`
177+
func NewClientFactory(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) *ClientFactory {
178+
`), 0644)
179+
assert.NoError(t, err)
180+
181+
err = UpdateReadmeClientFactory(tmpDir)
182+
assert.NoError(t, err)
183+
184+
withSubContent, err := os.ReadFile(filepath.Join(tmpDir, "README.md"))
185+
assert.NoError(t, err)
186+
assert.Contains(t, string(withSubContent), `NewClientFactory(<subscription ID>, cred, nil)`)
187+
assert.Contains(t, string(withSubContent), `NewClientFactory(<subscription ID>, cred, &options)`)
188+
}

eng/tools/generator/cmd/v2/common/generation.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ func (t *SwaggerCommonGenerator) AfterGenerate(generateParam *GenerateParam, cha
293293
return nil, err
294294
}
295295
}
296+
log.Printf("Update README.md ClientFactory...")
297+
err := UpdateReadmeClientFactory(t.PackagePath)
298+
if err != nil {
299+
// only log error, avoid breaking the process
300+
log.Printf("Update README.md ClientFactory failed! err: %v", err)
301+
}
296302
return nil, nil
297303
}
298304

@@ -670,6 +676,13 @@ func (t *TypeSpecCommonGenerator) AfterGenerate(generateParam *GenerateParam, ch
670676
if err := ExecuteGo(modulePath, "mod", "tidy"); err != nil {
671677
return nil, err
672678
}
679+
680+
log.Printf("Update README.md ClientFactory...")
681+
err := UpdateReadmeClientFactory(t.PackagePath)
682+
if err != nil {
683+
// only log error, avoid breaking the process
684+
log.Printf("Update README.md ClientFactory failed! err: %v", err)
685+
}
673686
return nil, nil
674687
}
675688

0 commit comments

Comments
 (0)