You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CG-10508: Docs explain differences between SourceFile and File types (#138)
# Motivation
SourceFile is currently not mentioned in the Building with Codegen
Section
# Content
Add content explaining the differences between SourceFile and File
- [x] I have added tests for my changes (N/A)
- [x] I have updated the documentation or added new documentation as
needed
- [x] I have read and agree to the [Contributor License
Agreement](../CLA.md)
---------
Co-authored-by: codegen-bot <[email protected]>
-[Directory](/api-reference/core/Directory) - Represents a directory in the codebase
12
13
13
-
Both of these expose a rich API for accessing and manipulating their contents.
14
+
<Info>
15
+
[SourceFile](/api-reference/core/SourceFile) is a subclass of [File](/api-reference/core/File) that provides additional functionality for source code files.
16
+
</Info>
14
17
15
18
This guide explains how to effectively use these classes to manage your codebase.
16
19
@@ -31,8 +34,10 @@ for file in codebase.files:
31
34
32
35
# Check if a file exists
33
36
exists = codebase.has_file("path/to/file.py")
37
+
34
38
```
35
39
40
+
36
41
These APIs are similar for [Directory](/api-reference/core/Directory), which provides similar methods for accessing files and subdirectories.
37
42
38
43
```python
@@ -50,61 +55,58 @@ dir = file.directory
50
55
exists = codebase.has_directory("path/to/dir")
51
56
```
52
57
53
-
## Working with Non-Code Files (README, JSON, etc.)
58
+
## Differences between SourceFile and File
54
59
55
-
By default, Codegen focuses on source code files (Python, TypeScript, etc). However, you can access all files in your codebase, including documentation, configuration, and other non-code files like README.md, package.json, or .env:
60
+
-[File](/api-reference/core/File) - a general purpose class that represents any file in the codebase including non-code files like README.md, .env, .json, image files, etc.
61
+
-[SourceFile](/api-reference/core/SourceFile) - a subclass of [File](/api-reference/core/File) that provides additional functionality for source code files written in languages supported by the [codegen-sdk](/introduction/overview) (Python, TypeScript, JavaScript, React).
56
62
57
-
```python
58
-
# Get all files in the codebase (including README, docs, config files)
59
-
files = codebase.files(extensions="*")
63
+
The majority of intended use cases involve using exclusively [SourceFile](/api-reference/core/SourceFile) objects as these contain code that can be parsed and manipulated by the [codegen-sdk](/introduction/overview). However, there may be cases where it will be necessary to work with non-code files. In these cases, the [File](/api-reference/core/File) class can be used.
60
64
61
-
# Print files that are not source code (documentation, config, etc)
By default, the `codebase.files` property will only return [SourceFile](/api-reference/core/SourceFile) objects. To include non-code files the `extensions='*'` argument must be used.
# Get all files in the codebase (including non-code files)
72
+
all_files= codebase.files(extensions="*")
75
73
```
76
74
77
-
These APIs are similar for [Directory](/api-reference/core/Directory), which provides similar methods for accessing files and subdirectories.
78
75
79
-
## Raw Content and Metadata
76
+
When getting a file with `codebase.get_file`, files ending in `.py, .js, .ts, .jsx, .tsx` are returned as [SourceFile](/api-reference/core/SourceFile) objects while other files are returned as [File](/api-reference/core/File) objects.
77
+
78
+
Furthermore, you can use the `isinstance` function to check if a file is a [SourceFile](/api-reference/core/SourceFile):
80
79
81
80
```python
82
-
# Grab raw file string content
83
-
content =file.content # For text files
84
-
print('Length:', len(content))
85
-
print('# of functions:', len(file.functions))
81
+
py_file = codebase.get_file("path/to/file.py")
82
+
ifisinstance(py_file, SourceFile):
83
+
print(f"File {py_file.filepath} is a source file")
86
84
87
-
# Access file metadata
88
-
name =file.name # Base name without extension
89
-
extension =file.extension # File extension with dot
90
-
filepath =file.filepath # Full relative path
91
-
dir=file.directory # Parent directory
85
+
# prints: `File path/to/file.py is a source file`
92
86
93
-
# Access directory metadata
94
-
name =dir.name # Base name without extension
95
-
path =dir.path # Full relative path from repository root
96
-
parent =dir.parent # Parent directory
87
+
mdx_file = codebase.get_file("path/to/file.mdx")
88
+
ifnotisinstance(mdx_file, SourceFile):
89
+
print(f"File {mdx_file.filepath} is a non-code file")
90
+
91
+
# prints: `File path/to/file.mdx is a non-code file`
97
92
```
98
93
94
+
<Note>
95
+
Currently, the codebase object can only parse source code files of one language at a time. This means that if you want to work with both Python and TypeScript files, you will need to create two separate codebase objects.
96
+
</Note>
97
+
99
98
## Accessing Code
100
99
101
-
Files and Directories provide several APIs for accessing and iterating over their code.
100
+
[SourceFiles](/api-reference/core/SourceFile) and [Directories](/api-reference/core/Directory) provide several APIs for accessing and iterating over their code.
102
101
103
102
See, for example:
104
103
105
-
-`.functions` ([File](/api-reference/core/File#functions) / [Directory](/api-reference/core/Directory#functions)) - All [Functions](../api-reference/core/Function) in the file/directory
106
-
-`.classes` ([File](/api-reference/core/File#classes) / [Directory](/api-reference/core/Directory#classes)) - All [Classes](../api-reference/core/Class) in the file/directory
107
-
-`.imports` ([File](/api-reference/core/File#imports) / [Directory](/api-reference/core/Directory#imports)) - All [Imports](../api-reference/core/Import) in the file/directory
104
+
-`.functions` ([SourceFile](/api-reference/core/SourceFile#functions) / [Directory](/api-reference/core/Directory#functions)) - All [Functions](/api-reference/core/Function) in the file/directory
105
+
-`.classes` ([SourceFile](/api-reference/core/SourceFile#classes) / [Directory](/api-reference/core/Directory#classes)) - All [Classes](/api-reference/core/Class) in the file/directory
106
+
-`.imports` ([SourceFile](/api-reference/core/SourceFile#imports) / [Directory](/api-reference/core/Directory#imports)) - All [Imports](/api-reference/core/Import) in the file/directory
107
+
-`.get_function(...)` ([SourceFile](/api-reference/core/SourceFile#get-function) / [Directory](/api-reference/core/Directory#get-function)) - Get a specific function by name
108
+
-`.get_class(...)` ([SourceFile](/api-reference/core/SourceFile#get-class) / [Directory](/api-reference/core/Directory#get-class)) - Get a specific class by name
109
+
-`.get_global_var(...)` ([SourceFile](/api-reference/core/SourceFile#get-global-var) / [Directory](/api-reference/core/Directory#get-global-var)) - Get a specific global variable by name
108
110
109
111
110
112
```python
@@ -142,22 +144,68 @@ if main_function:
142
144
print(f"Local var: {var.name} = {var.value}")
143
145
```
144
146
147
+
## Working with Non-Code Files (README, JSON, etc.)
148
+
149
+
By default, Codegen focuses on source code files (Python, TypeScript, etc). However, you can access all files in your codebase, including documentation, configuration, and other non-code [files](/api-reference/core/File) like README.md, package.json, or .env:
150
+
151
+
```python
152
+
# Get all files in the codebase (including README, docs, config files)
153
+
files = codebase.files(extensions="*")
154
+
155
+
# Print files that are not source code (documentation, config, etc)
You can frequently do bulk modifictions via the [`.edit(...)`](../api-reference/core/Editable#edit) method or [`.replace(...)`](../api-reference/core/File#replace) method.
234
+
You can frequently do bulk modifictions via the [`.edit(...)`](/api-reference/core/Editable#edit) method or [`.replace(...)`](/api-reference/core/File#replace) method.
187
235
188
236
<Note>
189
237
Most useful operations will have bespoke APIs that handle edge cases, update
@@ -192,7 +240,7 @@ You can frequently do bulk modifictions via the [`.edit(...)`](../api-reference/
192
240
193
241
## Moving and Renaming Files
194
242
195
-
Files can be manipulated through methods like [`File.update_filepath()`](../api-reference/core/File#update-filepath), [`File.rename()`](../api-reference/core/File#rename), and [`File.remove()`](../api-reference/core/File#remove):
243
+
Files can be manipulated through methods like [`File.update_filepath()`](/api-reference/core/File#update-filepath), [`File.rename()`](/api-reference/core/File#rename), and [`File.remove()`](/api-reference/core/File#remove):
196
244
197
245
```python
198
246
# Move/rename a file
@@ -216,7 +264,7 @@ for file in codebase.files:
216
264
217
265
## Directories
218
266
219
-
[`Directories`](/api-reference/core/Directory) expose a similar API to the [File](../api-reference/core/File.mdx) class, with the addition of the `subdirectories` property.
267
+
[`Directories`](/api-reference/core/Directory) expose a similar API to the [File](/api-reference/core/File.mdx) class, with the addition of the `subdirectories` property.
0 commit comments