44 "os"
55 "os/exec"
66 "path/filepath"
7+ "sort"
78 "strings"
89
910 "github.com/lgdd/lfr-cli/internal/conf"
@@ -23,14 +24,27 @@ func IsSupportedJavaVersion(javaVersion int) bool {
2324 return false
2425}
2526
26- func FetchClientExtensionSamples (destination string ) error {
27- clientExtensionsSamplesPath := filepath .Join (destination , conf .ClientExtensionSampleProjectName )
27+ func FetchAllClientExtensionSamples (destination string ) error {
28+ err := fetchClientExtensionSamples (destination , conf .ClientExtensionSampleProjectName )
29+ if err != nil {
30+ return err
31+ }
32+
33+ err = fetchClientExtensionSamples (destination , conf .ClientExtensionExtraSampleProjectName )
34+ if err != nil {
35+ return err
36+ }
37+ return nil
38+ }
39+
40+ func fetchClientExtensionSamples (destination , projectName string ) error {
41+ clientExtensionsSamplesPath := filepath .Join (destination , projectName )
2842
2943 // Clone & checkout if ~/.lfr/liferay-portal does not exist
30- if _ , err := os .Stat (filepath .Join (destination , conf . ClientExtensionSampleProjectName )); err != nil {
44+ if _ , err := os .Stat (filepath .Join (destination , projectName )); err != nil {
3145 var gitProject strings.Builder
3246 gitProject .WriteString ("https://github.com/lgdd/" )
33- gitProject .WriteString (conf . ClientExtensionSampleProjectName )
47+ gitProject .WriteString (projectName )
3448
3549 gitClone := exec .Command ("git" , "clone" , "--depth" , "1" , gitProject .String ())
3650 gitClone .Dir = destination
@@ -54,7 +68,7 @@ func updateClientExtensionSamples(path string) {
5468 }
5569}
5670
57- func HandleClientExtensionsOffline (configPath string ) {
71+ func HandleAllClientExtensionsOffline (configPath string ) {
5872 if _ , err := os .Stat (filepath .Join (configPath , conf .ClientExtensionSampleProjectName )); err != nil {
5973 logger .PrintWarn ("Couldn't fetch client extensions samples from GitHub.\n " )
6074 logger .Println ("Copying embedded versions from the CLI instead." )
@@ -79,25 +93,65 @@ func HandleClientExtensionsOffline(configPath string) {
7993 }
8094}
8195
96+ func handleClientExtensionsOffline (configPath , projectName string ) {
97+ if _ , err := os .Stat (filepath .Join (configPath , projectName )); err != nil {
98+ logger .PrintWarn ("Couldn't fetch client extensions samples from GitHub.\n " )
99+ logger .Println ("Copying embedded versions from the CLI instead." )
100+ err = fileutil .CreateDirsFromAssets ("tpl/client_extension" , configPath )
101+ if err != nil {
102+ logger .Fatal (err .Error ())
103+ }
104+
105+ err = fileutil .CreateFilesFromAssets ("tpl/client_extension" , configPath )
106+ if err != nil {
107+ logger .Fatal (err .Error ())
108+ }
109+
110+ oldGitDirectory := filepath .Join (configPath , projectName , "git" )
111+ newGitDirectory := filepath .Join (configPath , projectName , ".git" )
112+ if err := os .Rename (oldGitDirectory , newGitDirectory ); err != nil {
113+ logger .Fatal (err .Error ())
114+ }
115+ } else {
116+ logger .PrintWarn ("Couldn't update client extensions samples from GitHub.\n " )
117+ logger .Print ("Using latest versions fetched." )
118+ }
119+ }
120+
82121func GetClientExtensionSampleNames () []string {
83- if err := FetchClientExtensionSamples (conf .GetConfigPath ()); err != nil {
84- HandleClientExtensionsOffline (conf .GetConfigPath ())
122+ if err := FetchAllClientExtensionSamples (conf .GetConfigPath ()); err != nil {
123+ HandleAllClientExtensionsOffline (conf .GetConfigPath ())
85124 }
86125
87126 clientExtensionSamplesPath := filepath .Join (conf .GetConfigPath (), conf .ClientExtensionSampleProjectName )
88- sampleDirs , err := os .ReadDir (clientExtensionSamplesPath )
127+ officialSampleDirs , err := os .ReadDir (clientExtensionSamplesPath )
128+
129+ if err != nil {
130+ logger .Fatal (err .Error ())
131+ }
132+
133+ clientExtensionExtraSamplesPath := filepath .Join (conf .GetConfigPath (), conf .ClientExtensionExtraSampleProjectName )
134+ extraSampleDirs , err := os .ReadDir (clientExtensionExtraSamplesPath )
89135
90136 if err != nil {
91137 logger .Fatal (err .Error ())
92138 }
93139
94140 var samples []string
95141
96- for _ , sampleDir := range sampleDirs {
142+ for _ , sampleDir := range officialSampleDirs {
97143 if sampleDir .IsDir () && strings .Contains (sampleDir .Name (), conf .ClientExtensionSamplePrefix ) {
98144 samples = append (samples , strings .Split (sampleDir .Name (), conf .ClientExtensionSamplePrefix )[1 ])
99145 }
100146 }
101147
148+ for _ , sampleDir := range extraSampleDirs {
149+ if sampleDir .IsDir () && strings .Contains (sampleDir .Name (), conf .ClientExtensionExtraSamplePrefix ) {
150+ samples = append (samples , strings .Split (sampleDir .Name (), conf .ClientExtensionExtraSamplePrefix )[1 ])
151+ }
152+ }
153+
154+ sort .Sort (sort .StringSlice (samples ))
155+
102156 return samples
103157}
0 commit comments