Skip to content

Commit

Permalink
feat(ocaml): Add dune-project updater
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Apr 19, 2021
1 parent ec4a365 commit f8b7e2c
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions __snapshots__/dune-project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports['DuneProject updateContent updates version in dune-project file 1'] = `
(lang dune 2.0)
(name sample)
(version 0.6.0)
`
10 changes: 10 additions & 0 deletions src/releasers/ocaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {Changelog} from '../updaters/changelog';
// OCaml
import {Opam} from '../updaters/ocaml/opam';
import {EsyJson} from '../updaters/ocaml/esy-json';
import {DuneProject} from '../updaters/ocaml/dune-project';
import {ReleasePRConstructorOptions} from '..';

const notEsyLock = (path: string) => !path.startsWith('esy.lock');
Expand Down Expand Up @@ -85,6 +86,15 @@ export class OCaml extends ReleasePR {
);
});

updates.push(
new DuneProject({
path: this.addPath('dune-project'),
changelogEntry,
version: candidate.version,
packageName: packageName.name,
})
);

updates.push(
new Changelog({
path: this.addPath(this.changelogPath),
Expand Down
48 changes: 48 additions & 0 deletions src/updaters/ocaml/dune-project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {checkpoint, CheckpointType} from '../../util/checkpoint';
import {Update, UpdateOptions, VersionsMap} from '../update';
import {GitHubFileContents} from '../../github';

export class DuneProject implements Update {
path: string;
changelogEntry: string;
version: string;
versions?: VersionsMap;
packageName: string;
create: boolean;
contents?: GitHubFileContents;

constructor(options: UpdateOptions) {
this.create = false;
this.path = options.path;
this.changelogEntry = options.changelogEntry;
this.version = options.version;
this.packageName = options.packageName;
}
updateContent(content: string): string {
const oldVersion = content.match(/^\(version ([A-Za-z0-9_\-+.~]+)\)$/m);
if (oldVersion) {
checkpoint(
`updating ${this.path} from ${oldVersion[1]} to ${this.version}`,
CheckpointType.Success
);
}
return content.replace(
/^\(version ([A-Za-z0-9_\-+.~]+)\)$/m,
`(version ${this.version})`
);
}
}
40 changes: 40 additions & 0 deletions test/updaters/dune-project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {readFileSync} from 'fs';
import {resolve} from 'path';
import * as snapshot from 'snap-shot-it';
import {describe, it} from 'mocha';
import {DuneProject} from '../../src/updaters/ocaml/dune-project';

const fixturesPath = './test/updaters/fixtures';

describe('DuneProject', () => {
describe('updateContent', () => {
it('updates version in dune-project file', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './dune-project'),
'utf8'
).replace(/\r\n/g, '\n');
const version = new DuneProject({
path: 'dune-project',
changelogEntry: '',
version: '0.6.0',
packageName: '',
});
const newContent = version.updateContent(oldContent);
snapshot(newContent);
});
});
});
3 changes: 3 additions & 0 deletions test/updaters/fixtures/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(lang dune 2.0)
(name sample)
(version 0.5.0)

0 comments on commit f8b7e2c

Please sign in to comment.