|
| 1 | +--- |
| 2 | +output: github_document |
| 3 | +--- |
| 4 | + |
| 5 | +<!-- README.md is generated from README.Rmd. Please edit that file --> |
| 6 | + |
| 7 | +```{r, include = FALSE} |
| 8 | +knitr::opts_chunk$set( |
| 9 | + collapse = TRUE, |
| 10 | + comment = "#>", |
| 11 | + fig.path = "man/figures/README-", |
| 12 | + out.width = "100%" |
| 13 | +) |
| 14 | +``` |
| 15 | + |
| 16 | +# filestamp <img src="man/figures/filestamp-animated-logo.svg" width="170" align="right" alt="A hexagonal logo for filestamp"/> |
| 17 | + |
| 18 | +<!-- badges: start --> |
| 19 | +[](https://github.com/coatless-rpkg/filestamp/actions/workflows/R-CMD-check.yaml) |
| 20 | +<!-- badges: end --> |
| 21 | + |
| 22 | +`filestamp` makes it easy to add and maintain consistent headers across all files in your project, regardless of programming language. Headers can include copyright notices, file descriptions, authorship information, and other metadata. |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +You can install the development version of `filestamp` from [GitHub][filestamp-gh] with: |
| 27 | + |
| 28 | +```{r} |
| 29 | +#| label: setup |
| 30 | +#| eval: false |
| 31 | +# install.packages("remotes") |
| 32 | +
|
| 33 | +# From GitHub |
| 34 | +remotes::install_github("coatless-rpkg/filestamp") |
| 35 | +``` |
| 36 | + |
| 37 | +## Getting Started |
| 38 | + |
| 39 | +This section provides a few examples of how to use `filestamp` to add headers to your files. For more detailed information, please refer to the package documentation. |
| 40 | + |
| 41 | +### Loading the Package |
| 42 | + |
| 43 | +To use `filestamp`, you first need to load the package using the `library()` function. This will make all the functions and features of `filestamp` available for use in your R session. |
| 44 | + |
| 45 | +```{r} |
| 46 | +#| label: setup-usage |
| 47 | +library(filestamp) |
| 48 | +``` |
| 49 | + |
| 50 | +### Stamping a Single File |
| 51 | + |
| 52 | +You can use the `stamp_file()` function to add a header to a single file. The function takes the file path as an argument and can also accept a template name or a custom template. |
| 53 | + |
| 54 | +```{r} |
| 55 | +#| label: add-headers |
| 56 | +#| eval: false |
| 57 | +
|
| 58 | +# Stamp a file with the default template |
| 59 | +stamp_file("script.R") |
| 60 | +
|
| 61 | +# Stamp a file with a specific template |
| 62 | +stamp_file("script.py", template = "mit") |
| 63 | +
|
| 64 | +# Preview changes without modifying the file |
| 65 | +stamp_file("script.R", action = "dryrun") |
| 66 | +
|
| 67 | +# Create a backup before stamping |
| 68 | +stamp_file("important_script.R", action = "backup") |
| 69 | +``` |
| 70 | + |
| 71 | + |
| 72 | +### Stamping a Directory with Multiple Files |
| 73 | + |
| 74 | +You can use the `stamp_dir()` function to add headers to all files in a directory. You can specify a pattern to match specific file types, and you can also choose to stamp files in subdirectories. |
| 75 | + |
| 76 | +```{r} |
| 77 | +#| label: stamp-dir |
| 78 | +#| eval: false |
| 79 | +# Stamp all R files in a directory |
| 80 | +stamp_dir("src", pattern = "\\.R$") |
| 81 | +
|
| 82 | +# Stamp all files in a directory and subdirectories |
| 83 | +stamp_dir("project", recursive = TRUE) |
| 84 | +
|
| 85 | +# Stamp with a specific template |
| 86 | +stamp_dir("src", template = "gpl-3") |
| 87 | +``` |
| 88 | + |
| 89 | +## Templates |
| 90 | + |
| 91 | +### Using Built-in Templates |
| 92 | + |
| 93 | +The package comes with several built-in templates: |
| 94 | + |
| 95 | +- `default` - A simple header with copyright, author, and license information |
| 96 | +- `mit` - MIT License header |
| 97 | +- `gpl-3` - GNU General Public License header |
| 98 | + |
| 99 | +List available templates: |
| 100 | + |
| 101 | + |
| 102 | +```{r} |
| 103 | +#| label: stamp-templates |
| 104 | +stamp_templates() |
| 105 | +``` |
| 106 | + |
| 107 | +### Creating Custom Templates |
| 108 | + |
| 109 | +You can create your own custom templates using the `stamp_template_create()` function. This allows you to define the fields and content of the header. |
| 110 | + |
| 111 | +```{r} |
| 112 | +#| label: custom-template |
| 113 | +#| eval: false |
| 114 | +# Create a custom template |
| 115 | +my_template <- stamp_template_create( |
| 116 | + name = "my_custom", |
| 117 | + fields = stamp_template_describe( |
| 118 | + copyright = stamp_template_field("copyright", "MyCompany 2025", required = TRUE), |
| 119 | + author = stamp_template_field("author", "John Doe", required = TRUE) |
| 120 | + ), |
| 121 | + content = stamp_template_content( |
| 122 | + "Copyright (c) {{copyright}}", |
| 123 | + "Created by: {{author}}", |
| 124 | + "File: {{filename}}" |
| 125 | + ) |
| 126 | +) |
| 127 | +
|
| 128 | +# Use the custom template |
| 129 | +stamp_file("script.R", template = my_template) |
| 130 | +``` |
| 131 | + |
| 132 | +## Updating Existing Headers |
| 133 | + |
| 134 | +You can update existing headers in files using the `stamp_update()` function. This is useful for changing copyright years, adding new authors, or modifying other metadata. |
| 135 | + |
| 136 | +> [!IMPORTANT] |
| 137 | +> |
| 138 | +> Make sure to use the `stamp_update()` function carefully, as it will modify existing files. Always create backups before making changes. |
| 139 | +> |
| 140 | +> Some helper functions may not work as expected if the file does not contain a header |
| 141 | +> or if the header is not in the expected format. |
| 142 | +
|
| 143 | +```{r} |
| 144 | +#| label: existing-header |
| 145 | +#| eval: false |
| 146 | +# Update copyright years in existing headers |
| 147 | +stamp_update("old_script.R", list( |
| 148 | + copyright = stamp_update_helper_copyright_extend() |
| 149 | +)) |
| 150 | +
|
| 151 | +# Add a new author to existing headers |
| 152 | +stamp_update("collaborative_script.R", list( |
| 153 | + author = stamp_update_helper_author_add("Jane Smith") |
| 154 | +)) |
| 155 | +``` |
| 156 | + |
| 157 | +## Language Support |
| 158 | + |
| 159 | +The package supports many programming languages out of the box: |
| 160 | + |
| 161 | +```{r} |
| 162 | +#| label: language-supported |
| 163 | +#| eval: false |
| 164 | +# List supported languages |
| 165 | +languages() |
| 166 | +
|
| 167 | +# Register a custom language |
| 168 | +language_register( |
| 169 | + "kotlin", |
| 170 | + extensions = c("kt", "kts"), |
| 171 | + comment_single = "//", |
| 172 | + comment_multi_start = "/*", |
| 173 | + comment_multi_end = "*/" |
| 174 | +) |
| 175 | +``` |
| 176 | + |
| 177 | +## Variables |
| 178 | + |
| 179 | +You can use variables in your templates to customize the header content. The package provides several built-in variables, and you can also define your own. |
| 180 | + |
| 181 | +```{r} |
| 182 | +#| label: stamp-template-variables |
| 183 | +#| eval: false |
| 184 | +# List available variables |
| 185 | +stamp_variables_list() |
| 186 | +
|
| 187 | +# Add a custom variable |
| 188 | +stamp_variables_add("team", "Data Science") |
| 189 | +
|
| 190 | +# Set company name |
| 191 | +options(filestamp.company = "Acme Corp") |
| 192 | +``` |
| 193 | + |
| 194 | +## License |
| 195 | + |
| 196 | +AGPL (>= 3) |
| 197 | + |
| 198 | +[filestamp-gh]: https://github.com/coatless-rpkg/filestamp |
0 commit comments