Skip to content

Commit

Permalink
Dev (#3)
Browse files Browse the repository at this point in the history
* feat: makefile

* refactor: update logger

* chore: update workflwo

* feat: better logger

* fix: remove version as we are using gh cli

* chore: makes pre-release builds on dev now

* chore: fix workflow not getting proper version

* chore: fix workflow not having gh_token

* chore: fix release not working properly

* fix: remove useless fi
  • Loading branch information
Eyepan authored Sep 5, 2024
1 parent 8b84de6 commit 6d7122b
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 33 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Release

on:
push:
branches:
- dev
- master

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.23

- name: Get the current version
id: get_version
run: |
VERSION=$(gh release list --limit 1 | head -n 1 | awk '{print $2}')
echo "Current version is $VERSION"
echo "VERSION=$VERSION" >> $GITHUB_ENV
env:
GH_TOKEN: ${{ github.token }}

- name: Calculate new version
id: new_version
run: |
VERSION=${{ env.VERSION }}
IFS='.' read -r major minor patch <<<"$VERSION"
if [ "${{ github.ref }}" == "refs/heads/dev" ]; then
patch=$((patch+1))
elif [ "${{ github.ref }}" == "refs/heads/master" ]; then
minor=$((minor+1))
patch=0
fi
NEW_VERSION="$major.$minor.$patch"
echo "New version is $NEW_VERSION"
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
- name: Build binaries for Linux
run: GOARCH=amd64 GOOS=linux go build -o ./target/${{ github.event.repository.name }}-linux main.go

- name: Build binaries for Windows
run: GOARCH=amd64 GOOS=windows go build -o ./target/${{ github.event.repository.name }}-windows main.go

- name: Build binaries for MacOS
run: GOARCH=amd64 GOOS=darwin go build -o ./target/${{ github.event.repository.name }}-darwin main.go

- name: Create release
run: |
if [ "${{ github.ref }}" == "refs/heads/dev" ]; then
gh release create ${{ env.NEW_VERSION }} ./target/${{ github.event.repository.name }}-linux ./target/${{ github.event.repository.name }}-windows ./target/${{ github.event.repository.name }}-darwin --title "Release ${{ env.NEW_VERSION }}" --notes "Release generated by GitHub Actions" --prerelease
elif [ "${{ github.ref }}" == "refs/heads/master" ]; then
gh release create ${{ env.NEW_VERSION }} ./target/${{ github.event.repository.name }}-linux ./target/${{ github.event.repository.name }}-windows ./target/${{ github.event.repository.name }}-darwin --title "Release ${{ env.NEW_VERSION }}" --notes "Release generated by GitHub Actions"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pnpm-lock.yaml
*.lockb


main
target/

# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"cSpell.words": ["lockb", "packagejson", "softprops", "Typeflag"]
"cSpell.words": ["GOARCH", "lockb", "packagejson", "softprops", "Typeflag"]
}
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
BIN_NAME=yap
.DEFAULT_GOAL := run

build:
GOARCH=amd64 GOOS=darwin go build -o ./target/${BIN_NAME}-darwin main.go
GOARCH=amd64 GOOS=windows go build -o ./target/${BIN_NAME}-windows main.go
GOARCH=amd64 GOOS=linux go build -o ./target/${BIN_NAME}-linux main.go

run: build
./target/${BIN_NAME}-linux

build_and_run: build run

clean:
go clean
rimraf ./target

test:
go test ./...

test_coverage:
go test ./... -coverprofile=coverage.out

dep:
go mod downlaoad

vet:
go vet
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

37 changes: 12 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ import (
"github.com/Eyepan/yap/src/utils"
)

// Init application
func Init() {
}

func main() {
slog.SetLogLoggerLevel(slog.LevelWarn)
// Load configurations
Expand All @@ -28,15 +24,12 @@ func main() {
log.Fatalf("Failed to load configurations: %v", err)
}

var resolveCount = 0
var totalResolveCount = 0
var downloadCount = 0
var totalDownloadCount = 0
stats := logger.Stats{}

// Parse the command input (for now, assume 'install')
command := "install" // this would be dynamically set by the CLI parser
if command == "install" {
logger.PrettyPrintStats(resolveCount, totalResolveCount, downloadCount, totalDownloadCount)
stats.PrettyPrintStats()

// Parse package.json to get core dependencies
pkgJSON, err := packagejson.ParsePackageJSON()
Expand All @@ -55,19 +48,18 @@ func main() {

// Start download workers
numWorkers := runtime.NumCPU() // Number of concurrent download workers
go startDownloadWorkers(numWorkers, downloads, config, &resolveCount, &totalResolveCount, &downloadCount, &totalDownloadCount)
go startDownloadWorkers(numWorkers, downloads, config, &stats)

// Start resolving core dependencies
go func() {
for name, version := range packagejson.GetAllDependencies(&pkgJSON) {
pkg := types.Package{Name: name, Version: version}
lockBin.CoreDependencies = append(lockBin.CoreDependencies, pkg)
totalResolveCount += 1
logger.PrettyPrintStats(resolveCount, totalResolveCount, downloadCount, totalDownloadCount)
stats.IncrementTotalResolveCount()
wg.Add(1)
go func(pkg types.Package) {
defer wg.Done()
resolved, err := resolvePackage(pkg, config, &resolveCount, &totalResolveCount, &downloadCount, &totalDownloadCount, downloads)
resolved, err := resolvePackage(pkg, config, &stats, downloads)
if err != nil {
log.Printf("Failed to resolve package %s: %v", pkg.Name, err)
return
Expand All @@ -93,16 +85,14 @@ func main() {
}

// Resolving process
func resolvePackage(pkg types.Package, config types.Config, resolveCount *int, totalResolveCount *int, downloadCount *int, totalDownloadCount *int, downloads chan<- types.MPackage) (*types.MPackage, error) {
func resolvePackage(pkg types.Package, config types.Config, stats *logger.Stats, downloads chan<- types.MPackage) (*types.MPackage, error) {
slog.Info(fmt.Sprintf("Fetching metadata for %s@%s", pkg.Name, pkg.Version))
vmd, err := metadata.FetchVersionMetadata(pkg, config, false)
if err != nil {
log.Fatalln("failed while fetching the metadata", err)
}
slog.Info(fmt.Sprintf("Done fetching metadata for %s@%s", pkg.Name, pkg.Version))
*resolveCount += 1
logger.PrettyPrintStats(*resolveCount, *totalResolveCount, *downloadCount, *totalDownloadCount)

stats.IncrementResolveCount()
// Send package for downloading
downloads <- types.MPackage{Name: vmd.Name, Version: vmd.Version, Dist: vmd.Dist}

Expand All @@ -116,9 +106,8 @@ func resolvePackage(pkg types.Package, config types.Config, resolveCount *int, t

for depName, depVersion := range vmd.Dependencies {
depPkg := types.Package{Name: depName, Version: depVersion}
*totalResolveCount += 1
logger.PrettyPrintStats(*resolveCount, *totalResolveCount, *downloadCount, *totalDownloadCount)
subResolved, err := resolvePackage(depPkg, config, resolveCount, totalResolveCount, downloadCount, totalDownloadCount, downloads)
stats.IncrementTotalResolveCount()
subResolved, err := resolvePackage(depPkg, config, stats, downloads)
if err != nil {
return nil, err
}
Expand All @@ -129,20 +118,18 @@ func resolvePackage(pkg types.Package, config types.Config, resolveCount *int, t
}

// Downloading process with worker pool
func startDownloadWorkers(numWorkers int, downloads <-chan types.MPackage, config types.Config, resolveCount *int, totalResolveCount *int, downloadCount *int, totalDownloadCount *int) {
func startDownloadWorkers(numWorkers int, downloads <-chan types.MPackage, config types.Config, stats *logger.Stats) {
for i := 0; i < numWorkers; i++ {
go func() {
for pkg := range downloads {
*totalDownloadCount += 1
logger.PrettyPrintStats(*resolveCount, *totalResolveCount, *downloadCount, *totalDownloadCount)
stats.IncrementTotalDownloadCount()
slog.Info(fmt.Sprintf("Downloading tarball for %s@%s", pkg.Name, pkg.Version))
err := downloader.DownloadPackage(types.Package{Name: pkg.Name, Version: pkg.Version}, pkg.Dist.Tarball, config, false)
if err != nil {
log.Printf("Failed to download package %s: %v", pkg.Name, err)
continue
}
*downloadCount += 1
logger.PrettyPrintStats(*resolveCount, *totalResolveCount, *downloadCount, *totalDownloadCount)
stats.IncrementDownloadCount()
slog.Info(fmt.Sprintf("Done downloading tarball for %s@%s", pkg.Name, pkg.Version))
}
}()
Expand Down
32 changes: 27 additions & 5 deletions src/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,36 @@ package logger

import (
"fmt"
"log/slog"
)

func PrettyPrintStats(resolveCount int, totalResolveCount int, downloadCount int, totalDownloadCount int) {
type Stats struct {
resolveCount int
totalResolveCount int
downloadCount int
totalDownloadCount int
}

func (s *Stats) PrettyPrintStats() {
// TODO: figure out a way to implement proper printing without inputting all the four numbers all the time
fmt.Printf("\r🔍[%d/%d] 📦[%d/%d]", resolveCount, totalResolveCount, downloadCount, totalDownloadCount)
fmt.Printf("\r🔍[%d/%d] 🚚[%d/%d]", s.resolveCount, s.totalResolveCount, s.downloadCount, s.totalDownloadCount)
}

func (s *Stats) IncrementResolveCount() {
s.resolveCount += 1
s.PrettyPrintStats()
}

func (s *Stats) IncrementTotalResolveCount() {
s.totalResolveCount += 1
s.PrettyPrintStats()
}

func (s *Stats) IncrementDownloadCount() {
s.downloadCount += 1
s.PrettyPrintStats()
}

func InfoLogger(message string) {
slog.Info(fmt.Sprintf("%s\n", message))
func (s *Stats) IncrementTotalDownloadCount() {
s.totalDownloadCount += 1
s.PrettyPrintStats()
}

0 comments on commit 6d7122b

Please sign in to comment.