to_llm 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ada141bafdd63b00f12af14c5d5150a8de09deb030f6dd9605780af6acf2c01f
4
- data.tar.gz: 05ac140d21cd33acb30f88914673a695ea9859479d0d115d8154826c04ee090b
3
+ metadata.gz: 79e005ef579c63795093a10bc0318048894fdaef71f959eaac97ec46d9d12340
4
+ data.tar.gz: 297867c42b4a9caf4d803798edd51deae0fcf63815f47a3e742ddd60cbf918ab
5
5
  SHA512:
6
- metadata.gz: f63a54f041713178db91321fb2efd3fdce44103007da3de130f451df8892a93728cb72e816cf9f953627d44626fe2561e5bdfc464f7ca71628c47b6703c54960
7
- data.tar.gz: 046b4b51252bbe3d38203b9e5f61eebe7ebd8decc69057eb178690cb8f36d3cfec06748a7497ed1cc26147c15efd09cf7552db898450fa6decfcc2f6242048fd
6
+ metadata.gz: 3aaa9070c20e815f41aebb34bb933e4ac289e45b0640edb0abe80af02b818ed3e42d7c902194df3571363229c0b5fd839cd3fc3c8d90e04f6bed6f61f15523a1
7
+ data.tar.gz: 9bde15f9c6be326a6659fd7d6d497234de245928c59179c0d5bc2487c254d446228684be518cbca768ec5a479e1449de2410db75ac02ff545cfd1426b7f17e65
data/README.md CHANGED
@@ -1,166 +1,75 @@
1
1
  # to_llm
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/to_llm.svg)](https://badge.fury.io/rb/to_llm)
4
- [![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE.txt)
5
4
 
6
- **to_llm** is a lightweight Ruby gem that provides a simple set of tasks (or commands) to extract code from a Rails application into text files. This is useful if you want to feed your Rails codebase into a Large Language Model (LLM) or any text-based analysis tool.
7
-
8
- ---
9
-
10
- ## Table of Contents
11
-
12
- - [Features](#features)
13
- - [Installation](#installation)
14
- - [Usage](#usage)
15
- - [Commands](#commands)
16
- - [Configuration](#configuration)
17
- - [Examples](#examples)
18
- - [Contributing](#contributing)
19
- - [License](#license)
20
-
21
- ---
5
+ **to_llm** is a lightweight Ruby gem that allows you to extract code from your Rails application into text files. It’s especially handy if you want to feed your Rails codebase into a Large Language Model (LLM) or any text-based analysis tool.
22
6
 
23
7
  ## Features
24
8
 
25
- - **Simple extraction**: Automatically scans your Rails app for files in `app/models`, `app/views`, `app/controllers`, etc.
26
- - **Configurable**: By default, extracts `.rb`, `.erb`, `.js`, and `.yml` file types, but you can easily adjust.
27
- - **Rake tasks**: Provides a Rake task (`to_llm:extract`) to keep usage straightforward.
28
- - **Keeps files separate**: Creates separate `.txt` output files (e.g., `models.txt`, `views.txt`) for easier organization (or combine them, if you prefer).
29
-
30
- ---
9
+ - **Simple extraction**: Automatically scans core Rails directories (`app/models`, `app/controllers`, `app/views`, etc.).
10
+ - **Configurable**: Extracts `.rb`, `.erb`, `.js`, `.yml`, `.ts`, and `.tsx` by default; easy to adjust.
11
+ - **Rake tasks**: Single entry point for extracting everything or selective directories.
12
+ - **Flexible formats**: Outputs to `.txt` or `.md`.
31
13
 
32
14
  ## Installation
33
15
 
34
- Add this line to your Rails project’s `Gemfile`:
16
+ Add to your `Gemfile`:
35
17
 
36
18
  ```ruby
37
- gem 'to_llm', git: 'https://github.com/jcmaciel/to_llm.git'
19
+ gem 'to_llm'
38
20
  ```
39
-
40
- > Or using RubyGems
41
- > ```ruby
42
- > gem 'to_llm'
43
- > ```
44
-
45
- Then execute:
21
+ And then:
46
22
 
47
23
  ```bash
48
24
  bundle install
49
25
  ```
50
26
 
51
- ---
27
+ *(Alternatively, you can point `gem 'to_llm', git: 'https://github.com/jcmaciel/to_llm.git'` to the GitHub repo.)*
52
28
 
53
29
  ## Usage
54
30
 
55
- Once installed, the gem integrates with your Rails app via a Railtie that exposes one or more rake tasks. By default, you can run:
31
+ With **v0.1.3**, you can run the following commands inside your Rails app:
56
32
 
57
33
  ```bash
58
- rails to_llm:extract -[ALL|MODELS|CONTROLLERS|VIEWS|CONFIG|SCHEMA]
34
+ rails "to_llm:extract[TYPE,FORMAT]"
59
35
  ```
60
36
 
61
- Each of these commands will scan the relevant folders in your Rails app and produce text files containing all the code it finds.
62
-
63
- ---
64
-
65
- ## Commands
66
-
67
- ### 1. `rails to_llm:extract -ALL`
68
-
69
- - **Description**: Extracts from all supported Rails directories:
70
- - `app/models` -> `models.txt`
71
- - `app/controllers` -> `controllers.txt`
72
- - `app/views` -> `views.txt`
73
- - `app/helpers` -> `helpers.txt`
74
- - `config` -> `config.txt` (including `config/initializers`)
75
- - `db/schema.rb` -> `schema.txt`
76
- - **Result**: Creates a folder named `to_llm/` with separate `.txt` files.
77
-
78
- ### 2. `rails to_llm:extract -MODELS`
79
-
80
- - **Description**: Extracts only files from `app/models`.
81
- - **Output**: Creates (or overwrites) `to_llm/models.txt`.
82
-
83
- ### 3. `rails to_llm:extract -CONTROLLERS`
84
-
85
- - **Description**: Extracts only from `app/controllers`.
86
- - **Output**: `to_llm/controllers.txt`.
87
-
88
- ### 4. `rails to_llm:extract -VIEWS`
89
-
90
- - **Description**: Extracts from `app/views`.
91
- - **Output**: `to_llm/views.txt`.
92
-
93
- ### 5. `rails to_llm:extract -CONFIG`
94
-
95
- - **Description**: Extracts from `config`, including `config/initializers`.
96
- - **Output**: `to_llm/config.txt`.
97
-
98
- ### 6. `rails to_llm:extract -SCHEMA`
99
-
100
- - **Description**: Extracts only `db/schema.rb`.
101
- - **Output**: `to_llm/schema.txt`.
37
+ Where:
38
+ - **TYPE** can be `ALL`, `MODELS`, `CONTROLLERS`, `VIEWS`, `CONFIG`, `SCHEMA`, `JAVASCRIPT`, `HELPERS`.
39
+ - **FORMAT** can be `txt` or `md`.
102
40
 
103
- ---
41
+ **Examples**:
104
42
 
105
- ## Configuration
106
-
107
- If you want to customize what extensions are included or which directories map to which output files, open the `lib/tasks/to_llm.rake` file (inside this gem) and edit:
108
-
109
- ```ruby
110
- file_extensions = %w[.rb .erb .js .yml]
111
- directories_to_files = {
112
- "app/models" => "models.txt",
113
- "app/controllers" => "controllers.txt",
114
- # ...
115
- }
116
- ```
117
-
118
- This gem is intentionally minimalist. Feel free to fork or override these settings in your local app.
119
-
120
- ---
121
-
122
- ## Examples
123
-
124
- 1. **Extract everything**:
43
+ 1. Extract *everything* to Markdown:
125
44
  ```bash
126
- rails to_llm:extract -ALL
127
- ```
128
- Generates:
45
+ rails "to_llm:extract[ALL,md]"
129
46
  ```
130
- to_llm/
131
- models.txt
132
- controllers.txt
133
- views.txt
134
- helpers.txt
135
- config.txt
136
- schema.txt
137
- ```
138
-
139
- 2. **Extract only views**:
47
+ 2. Extract only models to plain text:
140
48
  ```bash
141
- rails to_llm:extract -VIEWS
142
- ```
143
- Generates:
49
+ rails "to_llm:extract[MODELS,txt]"
144
50
  ```
145
- to_llm/
146
- views.txt
147
- ```
148
- (Note that any previously existing files in `to_llm` will be untouched or overwritten if they have the same filename.)
51
+ 3. If you omit the second parameter, it defaults to `.txt` (old usage triggers a warning but still works).
52
+
53
+ ## Examples
54
+
55
+ - **Extract everything**:
56
+ ```bash
57
+ rails "to_llm:extract[ALL,md]"
58
+ ```
59
+ Produces a `to_llm/` folder with separate `.md` files (e.g., `models.md`, `views.md`, etc.).
149
60
 
150
- ---
61
+ - **Extract only controllers**:
62
+ ```bash
63
+ rails "to_llm:extract[CONTROLLERS,txt]"
64
+ ```
65
+ Creates/overwrites `to_llm/controllers.txt`.
151
66
 
152
67
  ## Contributing
153
68
 
154
- 1. Fork the repo on GitHub.
155
- 2. Create a new branch for your feature (`git checkout -b my-new-feature`).
69
+ 1. Fork the repository.
70
+ 2. Create a new branch (`git checkout -b my-new-feature`).
156
71
  3. Commit your changes (`git commit -am 'Add new feature'`).
157
72
  4. Push to your branch (`git push origin my-new-feature`).
158
- 5. Create a Pull Request on GitHub.
159
-
160
- We welcome issues, PRs, and general feedback!
161
-
162
- ---
163
-
164
- ## License
73
+ 5. Open a Pull Request on GitHub.
165
74
 
166
- This project is available as open source under the terms of the [MIT License](./LICENSE.txt). Feel free to use it in your own projects.
75
+ Feedback, issues, and PRs are always welcome!
@@ -1,39 +1,29 @@
1
+ # lib/tasks/to_llm.rake
1
2
  # frozen_string_literal: true
2
3
 
3
- #
4
- # rails to_llm:extract -ALL
5
- # rails to_llm:extract -MODELS
6
- # rails to_llm:extract -CONTROLLERS
7
- # rails to_llm:extract -VIEWS
8
- # rails to_llm:extract -CONFIG
9
- # rails to_llm:extract -SCHEMA
10
- #
11
-
12
-
13
4
  namespace :to_llm do
14
- desc "Extract code for LLM usage. Usage: rails to_llm:extract [ALL|MODELS|CONTROLLERS|VIEWS|CONFIG|SCHEMA]"
15
- task :extract, [:type] => :environment do |_t, args|
16
- # Default to "ALL" if no argument
17
- extract_type = args[:type]&.upcase || "ALL"
18
-
19
- puts "----- to_llm:extract started with type=#{extract_type} -----"
5
+ def run_extraction(extract_type, output_format)
6
+ extract_type = extract_type&.upcase || "ALL"
7
+ output_format = output_format&.downcase || "txt"
8
+
9
+ puts ">> to_llm:extract started with type=#{extract_type}, format=#{output_format}"
20
10
 
21
11
  # Directory where extracted files will be stored
22
12
  output_dir = "to_llm"
23
13
  FileUtils.mkdir_p(output_dir)
24
14
 
25
- # Define which directories to process
15
+
26
16
  directories_to_files = {
27
- "app/models" => "models.txt",
28
- "app/controllers" => "controllers.txt",
29
- "app/views" => "views.txt",
30
- "app/helpers" => "helpers.txt",
31
- "app/javascript" => "javascript.txt",
32
- "config" => "config.txt",
33
- "db/schema.rb" => "schema.txt"
17
+ "app/models" => "models",
18
+ "app/controllers" => "controllers",
19
+ "app/views" => "views",
20
+ "app/helpers" => "helpers",
21
+ "app/javascript" => "javascript",
22
+ "config" => "config",
23
+ "db/schema.rb" => "schema"
34
24
  }
35
25
 
36
- # We can filter which directories to process based on the user input
26
+
37
27
  filter_map = {
38
28
  "ALL" => %w[app/models app/controllers app/views app/helpers app/javascript config db/schema.rb],
39
29
  "MODELS" => %w[app/models],
@@ -41,50 +31,120 @@ namespace :to_llm do
41
31
  "VIEWS" => %w[app/views],
42
32
  "CONFIG" => %w[config],
43
33
  "SCHEMA" => %w[db/schema.rb],
44
- "JAVASCRIPT" => %w[app/javascript],
45
- "HELPERS" => %w[app/helpers]
34
+ "JAVASCRIPT" => %w[app/javascript],
35
+ "HELPERS" => %w[app/helpers]
46
36
  }
47
37
 
48
- # Define which extensions you want to extract
38
+ # File extensions to extract
49
39
  file_extensions = %w[.rb .erb .js .yml .ts .tsx]
50
40
 
51
- directories_to_files.values.each do |filename|
52
- file_path = File.join(output_dir, filename)
53
- File.delete(file_path) if File.exist?(file_path)
41
+ # Clean old outputs
42
+ directories_to_files.values.each do |base_filename|
43
+ path = File.join(output_dir, "#{base_filename}.#{output_format}")
44
+ File.delete(path) if File.exist?(path)
54
45
  end
55
46
 
56
- def extract_files(dir_path, file_extensions, output_file, output_dir)
57
- Dir.glob(File.join(dir_path, "**", "*")) do |path|
58
- next if File.directory?(path)
59
- next unless file_extensions.include?(File.extname(path))
60
-
61
- File.open(File.join(output_dir, output_file), "a") do |f|
62
- f.puts "#{path}:"
63
- f.puts "----------------------------------------------------"
64
- f.puts File.read(path)
65
- f.puts "\n"
66
- end
47
+ # Detects a language for syntax highlighting if output_format = md
48
+ def detect_language(ext)
49
+ case ext
50
+ when ".rb" then "ruby"
51
+ when ".erb" then "erb"
52
+ when ".js" then "javascript"
53
+ when ".ts", ".tsx" then "typescript"
54
+ when ".yml" then "yaml"
55
+ else "plaintext"
67
56
  end
68
57
  end
69
58
 
70
- (filter_map[extract_type] || []).each do |dir_or_file|
71
- next unless directories_to_files.key?(dir_or_file)
72
- output_name = directories_to_files[dir_or_file]
59
+ # Writes file content in either txt or md format
60
+ def write_content(path, content, output_format, output_file)
61
+ if output_format == "md"
62
+ language = detect_language(File.extname(path))
63
+ <<~MARKDOWN
64
+ ## #{path}
65
+ ```#{language}
66
+ #{content}
67
+ ```
68
+ MARKDOWN
69
+ else
70
+ # txt format
71
+ <<~TXT
72
+ #{path}:
73
+ ----------------------------------------------------
74
+ #{content}
75
+
76
+ TXT
77
+ end
78
+ end
73
79
 
80
+ # Method to handle directories or single-file extraction
81
+ def extract_content(dir_or_file, file_extensions, output_format, base_output_name, output_dir)
74
82
  if File.file?(dir_or_file)
75
- if File.exist?(dir_or_file)
76
- File.open(File.join(output_dir, output_name), "a") do |f|
77
- f.puts "#{dir_or_file}:"
78
- f.puts "----------------------------------------------------"
79
- f.puts File.read(dir_or_file)
80
- f.puts "\n"
81
- end
83
+
84
+ return unless File.exist?(dir_or_file)
85
+
86
+ content = File.read(dir_or_file)
87
+ formatted = write_content(dir_or_file, content, output_format, base_output_name)
88
+ File.open(File.join(output_dir, "#{base_output_name}.#{output_format}"), "a") do |f|
89
+ f.puts formatted
82
90
  end
83
91
  else
84
- extract_files(dir_or_file, file_extensions, output_name, output_dir)
92
+
93
+ Dir.glob(File.join(dir_or_file, "**", "*")) do |path|
94
+ next if File.directory?(path)
95
+ next unless file_extensions.include?(File.extname(path))
96
+
97
+ content = File.read(path)
98
+ formatted = write_content(path, content, output_format, base_output_name)
99
+ File.open(File.join(output_dir, "#{base_output_name}.#{output_format}"), "a") do |f|
100
+ f.puts formatted
101
+ end
102
+ end
85
103
  end
86
104
  end
87
105
 
88
- puts "----- to_llm:extract finished! Output is located in '#{output_dir}' -----"
106
+
107
+ (filter_map[extract_type] || []).each do |dir_or_file|
108
+ next unless directories_to_files.key?(dir_or_file)
109
+
110
+ base_output_name = directories_to_files[dir_or_file]
111
+ extract_content(dir_or_file, file_extensions, output_format, base_output_name, output_dir)
112
+ end
113
+
114
+ puts ">> to_llm:extract finished! See '#{output_dir}' for .#{output_format} files."
115
+ end
116
+
117
+
118
+ desc "Extract code into txt or md. Usage: rails \"to_llm:extract[TYPE,FORMAT]\"\n" \
119
+ "Examples:\n" \
120
+ " rails \"to_llm:extract[ALL,md]\"\n" \
121
+ " rails \"to_llm:extract[MODELS,txt]\""
122
+ task :extract, [:type_and_format] => :environment do |_t, args|
123
+ if args[:type_and_format].blank?
124
+ # No parameters passed -> show help message
125
+ puts "--------------------------------------------------------------------"
126
+ puts "Usage: rails \"to_llm:extract[TYPE,FORMAT]\""
127
+ puts "Types: ALL, MODELS, CONTROLLERS, VIEWS, CONFIG, SCHEMA, JAVASCRIPT, HELPERS"
128
+ puts "Format: txt or md (Markdown)"
129
+ puts "Examples:"
130
+ puts " rails \"to_llm:extract[ALL,md]\""
131
+ puts " rails \"to_llm:extract[MODELS,txt]\""
132
+ puts "--------------------------------------------------------------------"
133
+ next
134
+ end
135
+
136
+ # Attempt to parse "TYPE,FORMAT"
137
+ type, format = args[:type_and_format].split(",")
138
+
139
+ if format.nil?
140
+ # Old usage
141
+ puts "You are using the old usage: rails to_llm:extract #{type}"
142
+ puts "Please use: rails \"to_llm:extract[#{type},md]\" or rails \"to_llm:extract[#{type},txt]\"."
143
+
144
+ run_extraction(type, "txt")
145
+ else
146
+ # New usage
147
+ run_extraction(type, format)
148
+ end
89
149
  end
90
150
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ToLLM
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_llm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - José Carlos Maciel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-31 00:00:00.000000000 Z
11
+ date: 2025-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '5.0'
27
27
  description: A simple gem that provides a Rails command or Rake tasks to export .rb,
28
- .erb, .js, and .yml files into .txt for LLM ingestion.
28
+ .erb, .js, and .yml files into .txt or .md for LLM ingestion.
29
29
  email:
30
30
  - jcmacielp@gmail.com
31
31
  executables: []