Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
khanakia committed Oct 31, 2023
0 parents commit c560afb
Show file tree
Hide file tree
Showing 17 changed files with 11,529 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
vendor
.env

# ignore all files starting with . or ~
##.*
~*

# ignore OS generated files
.sass-cache
.DS_Store
ehthumbs.db
Thumbs.db
*.idea
*.TMP
*.tmp
.vscode-upload.json
node_modules/
yarn.lock

tmp/
pcu
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## pcu (php-composer-update)
Updates your composer.json dependencies to their latest versions.

## Usage

### Check the `./composer.json` file in current working directory and print the latest version information
```sh
pcu check
```

### Update the same `composer.json` file with latest version
```sh
pcu check -u
```


### Specify custom input and output path
```sh
pcu check --file=./test/composer.json --out=./out.json
```


## Sample Output
```js
laravel/jetstream ^4.0 ^4.0
mll-lab/laravel-graphiql ^3.0 ^3.1
mll-lab/graphql-php-scalars ^6.1 ^6.2
nuwave/lighthouse ^6.8 ^6.22
laravel/sanctum ^3.2 ^3.3
milon/barcode ^10.0 ^10.0
filament/filament ^3.0-stable ^3.0
guzzlehttp/guzzle ^7.2 ^7.8
hidehalo/nanoid-php ^1.1 ^1.1
barryvdh/laravel-dompdf ^2.0 ^2.0
egulias/email-validator ^4.0 ^4.0
laravel/framework ^10.0 ^10.30
doctrine/dbal ^3.6 ^3.7
commerceguys/addressing ^2.0 ^2.0
timehunter/laravel-google-recaptcha-v3 ^2.5 ^2.5
laravel/tinker ^2.8 ^2.8
plank/laravel-mediable ^5.9 ^5.9
```
69 changes: 69 additions & 0 deletions composer/composer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package composer

import (
"errors"
"fmt"
"os"
)

type ComposerFile struct {
Name string `json:"name"`
Type string `json:"type"`
Description string `json:"description"`
Keywords []string `json:"keywords"`
License string `json:"license"`
Require map[string]string `json:"require"`
RequireDev map[string]string `json:"require-dev"`
Autoload Autoload `json:"autoload"`
AutoloadDev Autoload `json:"autoload-dev"`
Scripts Scripts `json:"scripts"`
Repositories []Repositories `json:"repositories"`
Extra Extra `json:"extra"`
Config Config `json:"config"`
MinimumStability string `json:"minimum-stability"`
PreferStable bool `json:"prefer-stable"`
}

type Autoload struct {
Psr4 map[string]string `json:"psr-4"`
}

type Scripts struct {
PostAutoloadDump []string `json:"post-autoload-dump"`
PostUpdateCmd []string `json:"post-update-cmd"`
PostRootPackageInstall []string `json:"post-root-package-install"`
PostCreateProjectCmd []string `json:"post-create-project-cmd"`
}

type Repositories struct {
Type string `json:"type"`
URL string `json:"url"`
}

type Extra struct {
Laravel map[string]interface{} `json:"laravel"`
}

type Config struct {
OptimizeAutoloader bool `json:"optimize-autoloader"`
PreferredInstall string `json:"preferred-install"`
SortPackages bool `json:"sort-packages"`
AllowPlugins map[string]bool `json:"allow-plugins"`
}

func OpenFile(filepath string) ([]byte, error) {
info, err := os.Stat(filepath)

if errors.Is(err, os.ErrNotExist) || info.IsDir() {
fmt.Println("file does not exist")
return nil, err
}

fileBytes, err := os.ReadFile(filepath)
if err != nil {
fmt.Println("unable to read file")
return nil, err
}

return fileBytes, nil
}
Binary file added docs/demo.mp4
Binary file not shown.
9 changes: 9 additions & 0 deletions docs/dev.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Developer Documentation


## Testing
```
go test -v ./packagist
go test -v ./util
go test -v ./...
```
39 changes: 39 additions & 0 deletions githubrepo/githubrepo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package githubrepo

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)

func GetRepoLatestRelease(pkgname string) (*Repo, error) {
if len(pkgname) == 0 {
return nil, errors.New("package name is empty")
}

url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", pkgname)
resp, err := http.Get(url)
if err != nil {
return nil, errors.New("unable to fetch package info")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("unable to fetch package info")
}

var repo *Repo
err = json.Unmarshal(body, &repo)
if err != nil {
return nil, errors.New("unable to parse json")
}

return repo, nil
}

type Repo struct {
TargetCommitish string `json:"target_commitish"`
TagName string `json:"tag_name"`
Name string `json:"name"`
}
21 changes: 21 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module github.com/khanakia/pcu

go 1.21.3

require (
github.com/fatih/color v1.15.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/rodaine/table v1.1.0 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/ubgo/goutil v0.1.3 // indirect
golang.org/x/sys v0.6.0 // indirect
)
45 changes: 45 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rodaine/table v1.1.0 h1:/fUlCSdjamMY8VifdQRIu3VWZXYLY7QHFkVorS8NTr4=
github.com/rodaine/table v1.1.0/go.mod h1:Qu3q5wi1jTQD6B6HsP6szie/S4w1QUQ8pq22pz9iL8g=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/ubgo/goutil v0.1.3 h1:0WAkkvAwTbg4DzQQTx2yn5sqcAJL79XYv07/Vx95Yv4=
github.com/ubgo/goutil v0.1.3/go.mod h1:TXVTIT5wSE2PoGOOVf13DE8ZS1EALKGrYsdPxD52ehw=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
22 changes: 22 additions & 0 deletions jsonupdate/jsonupdate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package jsonupdate

import "github.com/tidwall/sjson"

type JsonUpdate struct {
jsondata string
}

func (s *JsonUpdate) String() string {
return s.jsondata
}

func (s *JsonUpdate) Set(key, value string) *JsonUpdate {
s.jsondata, _ = sjson.Set(s.jsondata, key, value)
return s
}

func NewJsonUpdate(jsondata string) *JsonUpdate {
return &JsonUpdate{
jsondata: jsondata,
}
}
98 changes: 98 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"encoding/json"
"fmt"
"os"

"github.com/khanakia/pcu/composer"
"github.com/khanakia/pcu/jsonupdate"
"github.com/khanakia/pcu/packagist"
"github.com/khanakia/pcu/util"
"github.com/spf13/cobra"
)

var version = "dev"

var (
update bool
)

func main() {
rootCmd.AddCommand(checkCmd)
checkCmd.PersistentFlags().StringP("file", "f", "./composer.json", "default to current working directory")
checkCmd.PersistentFlags().StringP("out", "o", "./composer.json", "output file path")
checkCmd.PersistentFlags().BoolVarP(&update, "update", "u", false, "udpate file")

if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

}

var rootCmd = &cobra.Command{
Use: "pcu",
Version: version,
Short: "php-composer-update updates your package.json dependencies to their latest versions, disregarding any specified version.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Run command `pcu --help` for more information`")
},
}

var checkCmd = &cobra.Command{
Use: "check",
Short: "Check dependencies version",
Run: func(cmd *cobra.Command, args []string) {
filepath, _ := cmd.Flags().GetString("file")
out, _ := cmd.Flags().GetString("out")
update, _ := cmd.Flags().GetBool("update")
composerCheck(filepath, out, update)
},
}

func composerCheck(filepath string, out string, update bool) {
fileBytes, err := composer.OpenFile(filepath)
if err != nil {
fmt.Println(err.Error())
return
}

jsonupd := jsonupdate.NewJsonUpdate(string(fileBytes))

var composerFile composer.ComposerFile
err = json.Unmarshal(fileBytes, &composerFile)
if err != nil {
fmt.Println("unable to parse comopser.json file")
}

for key, v := range composerFile.Require {
if packagist.ShouldSkip(key, v) {
continue
}
// fmt.Println(key)
versionName := packagist.FetchAndGetLastesVersionName(key)
newVersionName := util.TagNameConvert(versionName)
fmt.Printf("%-50.50s %-15s %s\n", key, v, newVersionName)

jsonupd.Set("require."+key, newVersionName)
}

for key, v := range composerFile.RequireDev {
if packagist.ShouldSkip(key, v) {
continue
}
// fmt.Println(key)
versionName := packagist.FetchAndGetLastesVersionName(key)
newVersionName := util.TagNameConvert(versionName)
fmt.Printf("%-50.50s %-15s %s\n", key, v, newVersionName)

jsonupd.Set("require-dev."+key, newVersionName)
}

if update {
_ = os.WriteFile(out, []byte(jsonupd.String()), 0644)
}

}
Loading

0 comments on commit c560afb

Please sign in to comment.