licensure 0.5.1

A software license management tool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# licensure

A software license header and copyright mangement tool.

## Table of Contents

- [Why use Licensure?]#why-use-licensure
- [Installation]#installation
- [Usage]#usage
  - [Example]#example
  - [Supported Filetypes]#supported-filetypes
- [Configuration]#configuration
- [Contributing]#contributing
- [License]#license

## Why use Licensure?

According to the Apache Software Foundation: [License headers allow
someone examining the file to know the terms for the work, even when
it is distributed without the rest of the distribution. Without a
licensing notice, it must be assumed that the author has reserved all
rights, including the right to copy, modify, and
redistribute.](http://www.apache.org/legal/src-headers.html#faq-whyheader)

It's an easy extra step to make sure that no matter what file someone
is looking at in your software they are aware of their rights and
yours. However maintaining and updating a copyright notice in every
source file can be a cumbersome exercise in patience. This is where
Licensure comes in.

Licensure makes it easy to maintain and update your copyright notices
across a project.

## Installation

### Install from Release (WIP)

1. Navigate to [the Releases Page]https://github.com/chasinglogic/licensure/releases
2. Find the tar ball for your platform / architecture. For example, on 64 bit
   Mac OSX, the archive is named `licensure_{version}_darwin_amd64.tar.gz`
3. Extract the tar ball
4. Put the licensure binary in your `$PATH`

### Install from Source

Licensure is available on crates.io and so can be installed with the following
command:

```
cargo install licensure
```

Make sure that you have a working Rust environment. Instructions for
setting one up can be found [on the rustup
website](https://rustup.rs). You can then install licensure from
source with the following commands:

```bash
git clone https://github.com/chasinglogic/licensure
cd licensure
cargo install --path .
```

If you need to update licensure via cargo you can then run the appropriate
`cargo install` command with the `--force` flag.

## Usage

Licensure searches for a config file `.licensure.yml` which will
inform it's operation. Licensure will not function without a config
file and if it cannot find one it will report this to you. 

The method for finding a config file [can be found in the
Configuration section of this
document.](#where-the-configuration-file-lives) For our purposes we
can generate a default config file with: `licensure
--generate-config`. This will write an example configuration file with
documentation comments into your current working directory. Note that
the default config file this generates is not functional immediately
as you must fill out the `licenses` section of the config. Licensure
does not want to make assumptions about the licensing of your project.

In the following examples we will assume your using a simple
configuration file such as the following:

```yaml
licenses:
  - files: any
    ident: GPL-3.0
    authors:
      - name: Mathew Robinson
        email: chasinglogic@gmail.com
    auto_template: true
    
comments:
  - columns: 80
    extension: any
    commenter:
      type: line
      comment_char: "#"
      trailing_lines: 0
```

With this config file if you create a source file named `test.py` with the contents:

```python
print("Hello world!")
```

We can then license this file with the command:

```
chasinglogic@galactica $ licensure test.py
```

Licensure will print out the licensed file as follows:

```python
# Copyright 2018 Mathew Robinson <chasinglogic@gmail.com>. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
print("Hello World!")
```

If we want to update the contents `test.py` instead of just printing
out the licensed file content we can give Licensure the `--in-place`
(or shortened form `-i`) flag:

```
chasinglogic@galactica $ licensure --in-place test.py
```

Licensure does some naive string comparison to determine if the header already
exists. If you try to run licensure against a file which already has the
generated license header it will skip it. You can see what files were
skipped by running licensure with `--verbose`:

```
chasinglogic@galactica $ licensure --in-place --verbose test.py
test.py already licensed
chasinglogic@galactica $
```

This makes it safe and convenient to run `licensure --in-place
--project` on the same project multiple times.

## Configuration

Licensure requires the use of a configuration file. This section will
explain all of the options of a configuration file, their definitions,
and some explanation of how they work. If you're instead looking for
the quickest way to get up and running you can generate a config with
`licensure --generate-config` which will give you a default config
file with documentation comments.

### Where the Configuration File lives

The configuration file is written in yaml and is searched for by
climbing the directory tree, starting at the current working
directory, for a file named `.licensure.yml`. If it is not found the
global configuration file located at
`$XDG_CONFIG_HOME/licensure/config.yml` (where `$XDG_CONFIG_HOME` is
`$HOME/.config` by default) will be used.

This essentially means that subdirectories can have their own
licensure configs and the order of precedence is closest config file
to the current working directory.

### Top Level Configuration Options

The Configuration File only has a two top level options: `exclude`,
and `change_in_place`. The other top level keys in the config file are
referred to as [Configuration Sections](#configuration-sections) and
make up the bulk of Licensure configuration.

#### change\_in\_place. 

Takes a boolean indicating whether to change files in place when
licensure is run. If this is set to `false` then when this config file
is in use files will never be updated in place, even if `--in-place`
is provided via the command line.

**Example Configuration:**

```yaml
change_in_place: true
```

#### exclude

Takes a list of strings that will be compiled as regexes to filter out
files from licensing.  Excludes passed via the command line flag will
be joined with any found in a config file.

**Example Configuration:**

```yaml
# Regexes which if matched by a file path will always be excluded from
# getting a license header
excludes:
  - \.gitignore
  - .*lock
  - \.git/.*
  - \.licensure\.yml
  - README.*
  - LICENSE.*
  - .*\.(md|rst|txt)
```

### Configuration Sections

Currently Licensure has two configuration sections: `licenses` and
`comments`. They configure the two axes Licensure uses to generate a
license header.

#### licenses

The licenses section is a list of license configuration
objects. License configuration objects define what licenses should
apply to what files, the templates for those licenses, and the
copyright holders of those files. A license configuration object has
the following form:

```yaml
# Either a regex or the string "any" to determine to what files this
# license should apply. It is common for projects to have files
# under multiple licenses or with multiple copyright holders. This
# provides the ability to automatically license files correctly
# based on their file paths.
#
# If "any" is provided all files will match this license.
files: any

# The license identifier, a list of common identifiers can be
# found at: https://spdx.org/licenses/ but existence of the ident
# in this list it is not enforced unless auto_template is set to
# true.
ident: MIT

# A list of authors who hold copyright over these files
authors:
  # Provide either your full name or company name for copyright purposes
  - name: Your Name Here
    # Optionally provide email for copyright purposes
    # email: you@yourdomain.com

# The template that will be rendered to generate the header before
# comment characters are applied. Available variables are:
#  - [year]: substituted with the current year.
#  - [name of author]: Substituted with name of the author and email
#    if provided. If email is provided the output appears as Full
#    Name <email@example.com>. If multiple authors are provided the
#    list is concatenated together with commas.
template: |
  Copyright [year] [name of author]. All rights reserved. Use of
  this source code is governed by the [ident] license that can be
  found in the LICENSE file.

# If auto_template is true then the template configuration is ignored
# and the SPDX API will be queried with the ident value to
# automatically determine the license header template. auto_template
# works best with licenses that have a standardLicenseHeader field
# defined in their license info JSON, if it is not then we will use
# the full licenseText to generate the header which works fine for
# short licenses like MIT but can be quite lengthy for other licenses
# like BSD-4-Clause. The above default template is valid for most
# licenses and is recommended for MIT, and BSD licenses. Common
# licenses that work well with the auto_template feature are GPL
# variants, and the Apache 2.0 license.
#
# Important Note: this means the ident must be a valid SPDX identifier
# auto_template: true
```

A common licenses section would look like:

```yaml
licenses:
  - files: any
    authors:
      - name: Mathew Robinson
        email: chasinglogic@gmail.com
        ident: MIT
    template: |
      Copyright [year] [name of author]. All rights reserved. Use of
      this source code is governed by the [ident] license that can be
      found in the LICENSE file.
```

##### Year ranges

You can specify a year range for your copyright instead by using the start_year
configuration option. If provided Licensure will put a year range in your
copyright. An example with a `start_year` value of `2019` would look like so:

```
Copyright 2019, 2024 ...
```

The `end_year` can also be specified but if omitted will automatically be
updated to be the current year per your local system time. 

###### Automated year ranges

If you want per-file year ranges or just automated ones you can opt in with the
`use_dynamic_year_ranges: true` setting. This will use `git` commit information
to determine a files created and last updated year. It will then license that
file with a year range specific to it based on the `git` information.

#### comments

The comments section is a list of comment configuration
objects. Comment configuration objects define how files with certain
extensions will be commented. They can also define the column width to
wrap the generated license header at. A comment configuration object
has the following fields: `extensions` (or `extension`), `columns`,
`commenter`,

##### Columns Configuration

The `columns` key specifies to what width the license header should be wrapped. Common values include: `80`, `100`, `120`.

Example:

```yaml
columns: 80
```

##### Extension Configuration

The extensions (or singular extension) field defines which file
extensions to apply the commenter to. If extension is the string 
"any" then all extensions will match this comment configuration.

Example use of any:

```yaml
extension: any
```

Example definition of explicit extensions

```yaml
extensions:
  - js
  - rs
  - go
```

##### Commenter Configuration

The commenter field defines the kind of commenter to
generate. There are two types of commenters: line and block.

A line commenter type will apply the `comment_char` to the beginning
of each line in the license header. It will then add empty newlines to
the end of the header equal to `trailing_lines`.

A block commenter type will add `start_block_char` as the first character
in the license header and add `end_block_char` as the last character
in the license header. When `per_line_char` is provided each line of
the header between the block start and end characters will be
line commented with the `per_line_char`.

If trailing_lines is omitted it's assumed to be 0.

###### Line Commenter Example

This is an example of a line commenter configuration. 

```yaml
commenter:
  type: line
  comment_char: "//"
  trailing_lines: 0
```

If this commenter is given the text:

```
A piece of text that
spans multiple lines
```

It would generate:

```
// A piece of text that 
// spans multiple lines
```

Note: when columns has a value the text may be re-wrapped to match the
column width.

###### Block Commenter Example

This is an example of a block commenter configuration. 

```yaml
commenter:
  type: block
  start_block_char: "/*\n"
  end_block_char: "*/"
  per_line_char: "*"
  trailing_lines: 0
```

If this commenter is given the text:

```
A piece of text that
spans multiple lines
```

It would generate:

```
/*
* A piece of text that 
* spans multiple lines
*/
```

Note: when columns has a value the text may be re-wrapped to match the
column width.

### A Complete Configuration Example 

The best up to date minimal example configuration is the one for
[licensure itself](https://github.com/chasinglogic/licensure/blob/master/.licensure.yml).

## Contributing

1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. :fire: Submit a pull request :D :fire:

## License

This code is distributed under the GNU General Public License

```
    Copyright (C) 2024 Mathew Robinson

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
```