Skip to content

Commit

Permalink
Apply exclusions to project structure and shell cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
gusanmaz committed Jul 25, 2024
1 parent ebda100 commit 0cb95c0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
5 changes: 3 additions & 2 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func NewDumpCmd() *cobra.Command {
var rootPath, outputPath, outputType string
var excludePatterns []string
var includeGit, includeNonText bool
var includeGit, includeNonText, showExcluded bool
var cmd = &cobra.Command{
Use: "dump",
Short: "Dump a local project to JSON or Markdown",
Expand Down Expand Up @@ -48,7 +48,7 @@ Example: -e "*.go @.gitignore" -e "utils/extension_language_map.json go.mod go.s
if outputType == "json" {
err = utils.SaveAsJSON(projectData, outputPath+".json", includeGit, includeNonText)
} else if outputType == "md" {
err = utils.SaveAsMarkdown(projectData, outputPath+".md", includeGit, includeNonText)
err = utils.SaveAsMarkdown(projectData, outputPath+".md", includeGit, includeNonText, showExcluded)
} else {
fmt.Fprintf(os.Stderr, "Invalid output type. Use 'json' or 'md'\n")
return
Expand All @@ -69,6 +69,7 @@ Example: -e "*.go @.gitignore" -e "utils/extension_language_map.json go.mod go.s
cmd.Flags().StringArrayVarP(&excludePatterns, "exclude", "e", []string{}, "Patterns to exclude files (Use @ for file-based patterns, e.g., @.gitignore)")
cmd.Flags().BoolVar(&includeGit, "include-git", false, "Include .git files and directories")
cmd.Flags().BoolVar(&includeNonText, "include-non-text", false, "Include non-text files")
cmd.Flags().BoolVar(&showExcluded, "show-excluded", false, "Show excluded files in project structure and shell commands")

return cmd
}
11 changes: 6 additions & 5 deletions cmd/github2file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func NewGitHub2FileCmd() *cobra.Command {
var repoURL, outputType, outputDir, outputName, githubToken string
var excludePatterns []string
var allRepos, useGit, includeGit, includeNonText bool
var allRepos, useGit, includeGit, includeNonText, showExcluded bool
var cmd = &cobra.Command{
Use: "github2file",
Short: "Fetch a GitHub repository and save as JSON or Markdown",
Expand Down Expand Up @@ -56,10 +56,10 @@ Example: -e "*.go @.gitignore" -e "utils/extension_language_map.json go.mod go.s
}

for _, repo := range repos {
fetchAndSaveRepo(fmt.Sprintf("https://github.com/%s/%s", owner, repo.Name), outputType, outputDir, outputName, gitIgnore, useGit, githubToken, includeGit, includeNonText)
fetchAndSaveRepo(fmt.Sprintf("https://github.com/%s/%s", owner, repo.Name), outputType, outputDir, outputName, gitIgnore, useGit, githubToken, includeGit, includeNonText, showExcluded)
}
} else {
fetchAndSaveRepo(repoURL, outputType, outputDir, outputName, gitIgnore, useGit, githubToken, includeGit, includeNonText)
fetchAndSaveRepo(repoURL, outputType, outputDir, outputName, gitIgnore, useGit, githubToken, includeGit, includeNonText, showExcluded)
}
},
}
Expand All @@ -74,11 +74,12 @@ Example: -e "*.go @.gitignore" -e "utils/extension_language_map.json go.mod go.s
cmd.Flags().StringVarP(&githubToken, "token", "k", "", "GitHub API token")
cmd.Flags().BoolVar(&includeGit, "include-git", false, "Include .git files and directories")
cmd.Flags().BoolVar(&includeNonText, "include-non-text", false, "Include non-text files")
cmd.Flags().BoolVar(&showExcluded, "show-excluded", false, "Show excluded files in project structure and shell commands")

return cmd
}

func fetchAndSaveRepo(repoURL, outputType, outputDir, outputName string, gitIgnore *ignore.GitIgnore, useGit bool, githubToken string, includeGit, includeNonText bool) {
func fetchAndSaveRepo(repoURL, outputType, outputDir, outputName string, gitIgnore *ignore.GitIgnore, useGit bool, githubToken string, includeGit, includeNonText, showExcluded bool) {
owner, repo, path, err := utils.ParseGitHubURL(repoURL)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing GitHub URL: %v\n", err)
Expand All @@ -103,7 +104,7 @@ func fetchAndSaveRepo(repoURL, outputType, outputDir, outputName string, gitIgno
if outputType == "json" {
err = utils.SaveAsJSON(projectData, outputPath, includeGit, includeNonText)
} else if outputType == "md" {
err = utils.SaveAsMarkdown(projectData, outputPath, includeGit, includeNonText)
err = utils.SaveAsMarkdown(projectData, outputPath, includeGit, includeNonText, showExcluded)
} else {
fmt.Fprintf(os.Stderr, "Invalid output type. Use 'json' or 'md'\n")
return
Expand Down
5 changes: 3 additions & 2 deletions cmd/json2md.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func NewJSON2MDCmd() *cobra.Command {
var jsonPath, outputPath string
var includeGit, includeNonText bool
var includeGit, includeNonText, showExcluded bool
var cmd = &cobra.Command{
Use: "json2md",
Short: "Convert JSON to Markdown",
Expand All @@ -31,7 +31,7 @@ func NewJSON2MDCmd() *cobra.Command {
return
}

markdown := utils.GenerateMarkdown(projectData, includeGit, includeNonText)
markdown := utils.GenerateMarkdown(projectData, includeGit, includeNonText, showExcluded)

err = ioutil.WriteFile(outputPath, []byte(markdown), 0644)
if err != nil {
Expand All @@ -47,6 +47,7 @@ func NewJSON2MDCmd() *cobra.Command {
cmd.Flags().StringVarP(&outputPath, "output", "o", "project_structure.md", "Output Markdown file")
cmd.Flags().BoolVar(&includeGit, "include-git", false, "Include .git files and directories")
cmd.Flags().BoolVar(&includeNonText, "include-non-text", false, "Include non-text files")
cmd.Flags().BoolVar(&showExcluded, "show-excluded", false, "Show excluded files in project structure and shell commands")

return cmd
}
5 changes: 3 additions & 2 deletions cmd/pypi2file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func NewPyPI2FileCmd() *cobra.Command {
var packageName, outputType, outputDir, outputName string
var excludePatterns []string
var includeGit, includeNonText bool
var includeGit, includeNonText, showExcluded bool
var cmd = &cobra.Command{
Use: "pypi2file",
Short: "Fetch a PyPI package and save as JSON or Markdown",
Expand Down Expand Up @@ -55,7 +55,7 @@ func NewPyPI2FileCmd() *cobra.Command {
if outputType == "json" {
err = utils.SaveAsJSON(projectData, outputPath, includeGit, includeNonText)
} else if outputType == "md" {
err = utils.SaveAsMarkdown(projectData, outputPath, includeGit, includeNonText)
err = utils.SaveAsMarkdown(projectData, outputPath, includeGit, includeNonText, showExcluded)
} else {
fmt.Fprintf(os.Stderr, "Invalid output type. Use 'json' or 'md'\n")
return
Expand All @@ -77,6 +77,7 @@ func NewPyPI2FileCmd() *cobra.Command {
cmd.Flags().StringArrayVarP(&excludePatterns, "exclude", "e", []string{}, "Patterns to exclude files (Use @ for file-based patterns, e.g., @.gitignore)")
cmd.Flags().BoolVar(&includeGit, "include-git", false, "Include .git files and directories")
cmd.Flags().BoolVar(&includeNonText, "include-non-text", false, "Include non-text files")
cmd.Flags().BoolVar(&showExcluded, "show-excluded", false, "Show excluded files in project structure and shell commands")

cmd.MarkFlagRequired("package")

Expand Down
22 changes: 11 additions & 11 deletions utils/markdown_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
"strings"
)

func GenerateMarkdown(projectData ProjectData, includeGit, includeNonText bool) string {
func GenerateMarkdown(projectData ProjectData, includeGit, includeNonText, showExcluded bool) string {
var md strings.Builder

md.WriteString("# Project Structure\n\n")
md.WriteString("```\n")
md.WriteString(generateProjectTree(projectData, includeGit, includeNonText))
md.WriteString(generateProjectTree(projectData, includeGit, includeNonText, showExcluded))
md.WriteString("```\n\n")

md.WriteString("## Shell Commands to Create Project Structure\n\n")
md.WriteString("```bash\n")
md.WriteString(GenerateShellCommands(projectData, includeGit, includeNonText))
md.WriteString(GenerateShellCommands(projectData, includeGit, includeNonText, showExcluded))
md.WriteString("```\n\n")

md.WriteString("## File Contents\n\n")
Expand All @@ -33,18 +33,18 @@ func GenerateMarkdown(projectData ProjectData, includeGit, includeNonText bool)
return md.String()
}

func generateProjectTree(projectData ProjectData, includeGit, includeNonText bool) string {
func generateProjectTree(projectData ProjectData, includeGit, includeNonText, showExcluded bool) string {
var tree strings.Builder
tree.WriteString(".\n")

var allPaths []string
for _, dir := range projectData.Directories {
if includeGit || !strings.HasPrefix(dir, ".git") {
if (includeGit || !strings.HasPrefix(dir, ".git")) && (showExcluded || dir != "") {
allPaths = append(allPaths, dir)
}
}
for _, file := range projectData.Files {
if (includeGit || !strings.HasPrefix(file.Path, ".git")) && (includeNonText || isTextFile(file.Path)) {
if (includeGit || !strings.HasPrefix(file.Path, ".git/")) && (includeNonText || isTextFile(file.Path)) && (showExcluded || file.Content != "") {
allPaths = append(allPaths, file.Path)
}
}
Expand All @@ -66,17 +66,17 @@ func generateProjectTree(projectData ProjectData, includeGit, includeNonText boo
return tree.String()
}

func GenerateShellCommands(projectData ProjectData, includeGit, includeNonText bool) string {
func GenerateShellCommands(projectData ProjectData, includeGit, includeNonText, showExcluded bool) string {
var commands strings.Builder

for _, dir := range projectData.Directories {
if includeGit || !strings.HasPrefix(dir, ".git") {
if (includeGit || !strings.HasPrefix(dir, ".git")) && (showExcluded || dir != "") {
commands.WriteString(fmt.Sprintf("mkdir -p \"%s\"\n", dir))
}
}

for _, file := range projectData.Files {
if (includeGit || !strings.HasPrefix(file.Path, ".git/")) && (includeNonText || isTextFile(file.Path)) {
if (includeGit || !strings.HasPrefix(file.Path, ".git/")) && (includeNonText || isTextFile(file.Path)) && (showExcluded || file.Content != "") {
dir := filepath.Dir(file.Path)
if dir != "." {
commands.WriteString(fmt.Sprintf("mkdir -p \"%s\"\n", dir))
Expand All @@ -88,7 +88,7 @@ func GenerateShellCommands(projectData ProjectData, includeGit, includeNonText b
return commands.String()
}

func SaveAsMarkdown(projectData ProjectData, outputPath string, includeGit, includeNonText bool) error {
markdown := GenerateMarkdown(projectData, includeGit, includeNonText)
func SaveAsMarkdown(projectData ProjectData, outputPath string, includeGit, includeNonText, showExcluded bool) error {
markdown := GenerateMarkdown(projectData, includeGit, includeNonText, showExcluded)
return ioutil.WriteFile(outputPath, []byte(markdown), 0644)
}

0 comments on commit 0cb95c0

Please sign in to comment.