Atomist
The Composition
Published in
2 min readJan 7, 2019

--

’Tis the season to update the copyright date in all the
files you edit. Unfortunately, updating the copyright date in a file
is a detail that is easy to forget, especially as the year wears on.
Fortunately, it is something that is easy to automate. You could
write a macro in your favorite editor, which is nice for you, or you
could fix it for your whole organization with Atomist. For
obvious reasons, we chose the latter. It’s just a few lines of code:

async function addHeaderTransform(
p: Project,
ci: PushAwareParametersInvocation,
): Promise {

// process all the files in the project
await projectUtils.doWithFiles(p, ci.parameters.glob, async f => {
// except those matching the exclude glob pattern
if (ci.parameters.excludeGlob && minimatch(f.path, ci.parameters.excludeGlob)) {
return;
}
// only update files changed in the last push
if (!ci.push.filesChanged.includes(f.path)) {
return;
}
const content = await f.getContent();
// update the header
const newContent = upsertHeader(ci.parameters.header, content);
if (newContent !== content) {
await f.setContent(newContent);
}
return;
});
return p;
}

The upsertHeader function used to insert or update the license
header uses standard string manipulation. When your software delivery
machine (SDM) registers the above addHeaderTransform function as an
"autofix", it gets run on every push of every repositories in your
organization (or any subset of repositories you choose), making sure
the license header has the current year. The cloning of the
repository and committing any changes is handled by the SDM framework.

SDM autofixes can be used for a variety of tasks like this: formatting
files, fixing lint issues, updating documentation, and many more. Be
a good citizen and use Atomist to keep your source code files up to
date!

--

--