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

Code changes used for first draft of code generation #35

Merged
merged 11 commits into from
Nov 15, 2023
15 changes: 11 additions & 4 deletions .github/workflows/build-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,31 @@ jobs:

- uses: actions/[email protected]
with:
go-version: '1.20.4'
go-version: '1.21.4'

- name: Install kiota
run: dotnet tool install --global Microsoft.OpenApi.Kiota --version 1.8.1

- name: Clone the existing go-sdk
run: |
cd ../
git clone https://github.com/octokit/go-sdk.git

- name: Download latest schema
run: go run schemas/main.go --schema-next=false

- name: Run generation
run: time kiota generate -l go --ll trace -o generated/go -n kiota -d schemas/downloaded.json
run: kiota generate -l go --ll trace -o $(pwd)/../go-sdk/github/octokit -n octokit -d schemas/downloaded.json

- name: Build post-processing
run: go build -o post-processors/go/post-processor post-processors/go/main.go

- name: Run post-processing
run: post-processors/go/post-processor $(pwd)/generated/go
run: |
cd ../go-sdk
../source-generator/post-processors/go/post-processor $(pwd)

- name: Build generated and processed SDK
run: |
cd generated/go
cd ../go-sdk
go build ./...
53 changes: 32 additions & 21 deletions post-processors/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func run() error {

fileContents = fixImports(fileContents)
fileContents = fixCreateDateOnlyFromDiscriminatorValue(fileContents, file.Name())
fileContents = fixPackageNameInAPIClient(fileContents, file.Name())
fileContents = removeModelsKiotaDoesNotCleanUp(fileContents)
fileContents = dirtyHackForVersionsRequestBuilder(fileContents, file.Name())
fileContents = fixKiotaNonDeterminism(fileContents, file.Name())
Expand All @@ -55,8 +54,17 @@ func run() error {
}
}

// after files are written, initialize a module
cmd := exec.Command("go", "mod", "init", "github.com/octokit/kiota")
err = os.Remove(filepath.Join(dirPath, "go.mod"))
if err != nil {
return fmt.Errorf("could not remove go.mod file: %v", err)
}

err = os.Remove(filepath.Join(dirPath, "go.sum"))
if err != nil {
return fmt.Errorf("could not remove go.sum file: %v", err)
}

cmd := exec.Command("go", "mod", "init", "github.com/octokit/go-sdk")
cmd.Dir = dirPath

var stdout, stderr bytes.Buffer
Expand Down Expand Up @@ -114,6 +122,17 @@ func run() error {
fmt.Printf("installed dependency %s\n", dep)
}

cmd = exec.Command("go", "mod", "tidy")
cmd.Dir = dirPath

stdout.Reset()
stderr.Reset()

output, err = cmd.Output()
if err != nil {
fmt.Printf("could not run go mod tidy: %v\nfull error log:\n%s", err, stderr.String())
}

return nil
}

Expand All @@ -125,7 +144,12 @@ func walkFiles(path string, info fs.FileInfo, err error) error {
return fmt.Errorf("error walking files: %v", err)
}

// skip directories
// skip the .git directory
if strings.Contains(path, ".git") {
return nil
}

// skip other non-dot directories
if info.IsDir() {
return nil
}
Expand All @@ -135,9 +159,9 @@ func walkFiles(path string, info fs.FileInfo, err error) error {

// these fixes are working around bugs or limitations in Kiota and/or our schema
func fixImports(inputFile string) string {
// find: kiota/
// replace: github.com/octokit/kiota/
inputFile = strings.ReplaceAll(inputFile, `"kiota/`, `"github.com/octokit/kiota/`)
// find: go-sdk/
// replace: github.com/octokit/go-sdk/
inputFile = strings.ReplaceAll(inputFile, `"octokit/`, `"github.com/octokit/go-sdk/github/octokit/`)
return inputFile
}

Expand Down Expand Up @@ -198,7 +222,7 @@ func fixCreateDateOnlyFromDiscriminatorValue(inputFile string, filename string)
}

toReplace = `res, err := m.BaseRequestBuilder.RequestAdapter.SendCollection(ctx, requestInfo, i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.CreateDateOnlyFromDiscriminatorValue, errorMapping)`
replaceWith = `res, err := m.BaseRequestBuilder.RequestAdapter.SendCollection(ctx, requestInfo, i158396662f32fe591e8faa247af18558546841dba91f24f5c824e11e34188830.CreateKeySimpleFromDiscriminatorValue, errorMapping)`
replaceWith = `res, err := m.BaseRequestBuilder.RequestAdapter.SendCollection(ctx, requestInfo, i8bb20811a612dd15efa26f086111481a68f72cd9ac5da7a939a417131078d77e.CreateKeySimpleFromDiscriminatorValue, errorMapping)`

if strings.Contains(inputFile, toReplace) {
inputFile = strings.ReplaceAll(inputFile, toReplace, replaceWith)
Expand All @@ -207,19 +231,6 @@ func fixCreateDateOnlyFromDiscriminatorValue(inputFile string, filename string)
return inputFile
}

func fixPackageNameInAPIClient(inputFile string, filename string) string {
if !strings.Contains(filename, "api_client.go") {
return inputFile
}
toReplace := `package kiota`
replaceWith := `package main`

if strings.Contains(inputFile, toReplace) {
inputFile = strings.ReplaceAll(inputFile, toReplace, replaceWith)
}
return inputFile
}

func fixKiotaNonDeterminism(inputFile string, fileName string) string {
if !strings.Contains(fileName, "item_starred_repository.go") {
return inputFile
Expand Down
12 changes: 4 additions & 8 deletions scripts/generate-go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

# convenience script for local development only
# .github/workflows/build-go.yml is the source of truth for the generated Go workflow
# note: before running this script, ensure the generated/go directory has been deleted
# but the sample app restored:
# rm -rf generated/go/
# git restore generated/
# requires cloning github.com/octokit/go-sdk in a sibling directory

./scripts/install-tools.sh

set -ex

go run schemas/main.go --schema-next=false
kiota generate -l go --ll trace -o generated/go -n kiota -d schemas/downloaded.json
kiota generate -l go --ll trace -o $(pwd)/../go-sdk/github/octokit -n octokit -d schemas/downloaded.json
go build -o post-processors/go/post-processor post-processors/go/main.go
post-processors/go/post-processor $(pwd)/generated/go
cd generated/go
cd $(pwd)/../go-sdk
$(pwd)/../source-generator/post-processors/go/post-processor $(pwd)
go build ./...
go run *.go