muratgozel / node-calver

Calendar based software versioning library as node.js module and with cli support. 📅
MIT License
28 stars 5 forks source link

Behaviour of the 'micro' inc function #2

Closed Duncank closed 3 years ago

Duncank commented 3 years ago

When I make use of date-based elements in my version tag, I would expect Calver to automatically update these correctly.

Take this example:

const version = new Calver('YYYY.MM.MICRO', '2020.11.3');
version.inc(); // 2020.12.0

const version2 = new Calver('YYYY.MM.MICRO', '2020.12.0');
version2.inc(); // 2020.12.0

The first version number was last created in november (2020-11), since it is december now, it updates to 2020.12.0. If I want to increase the version number again in december, executing the inc() function returns exactly the same version number. Instead, we'll have to use inc('micro') to get 2020.12.1.

Maybe I'm missing something, but it seems to me that calver-based version numbers always should be adding one to the micro-level in this case, since the year and month levels did not change from the current version number. Calver knows what day it is, so it can compare it with the current version name to determine what should be updated.

I would expect the following behaviour:

const version = new Calver('YYYY.MM.MICRO', '2020.11.3');

// It is november 15th
version.inc(); // 2020.11.4 

// It is november 30th
version.inc(); // 2020.11.5

// It is december 1st
version.inc(); // 2020.12.0

// a later day in december
version.inc(); // 2020.12.1
muratgozel commented 3 years ago

Thank you. I also think this is what it should be by default. Fix is on the way...

muratgozel commented 3 years ago

Published the fix. Closing the issue now.

Duncank commented 3 years ago

Thanks for your help! But I'm not sure it is working as I described above. Can you explain what you expect to happen here:

var version = new calver('YYYY.MM.MICRO', '2020.12.5');
version.inc();
console.log(version.get());

I would assume this should return 2020.12.6, since we are in the same year and month, But instead it returns 2020.12.5.

muratgozel commented 3 years ago

I've published another update. Can you confirm that now it's working as expected?

Duncank commented 3 years ago

Yeah, this is it! thanks!

var version = new calver('YYYY.MM.MICRO', '2020.11.5');
version.inc(); // 2020.12.0

var version = new calver('YYYY.MM.MICRO', '2020.12.5');
version.inc(); // 2020.12.6
kherock commented 3 years ago

It seems like the API has changed a lot since this issue was opened. Is this is no longer possible with the current versions?

I'm looking for away to do a calendar increment that falls back to incrementing the micro level when the date is the same.

Currently, it just throws an error when the date components aren't updated:

const format = 'YYYY.MM.MICRO';

calver.inc(format, '2021.5.1', 'calendar'); // 2021.6.0
calver.inc(format, '2021.6.0', 'calendar'); // throws with 'There is no change in the version.'
calver.inc(format, '2021.6.0', 'calendar.micro'); // throws with 'Second level should be a modifier or remove it.'

I'd prefer if the API worked this way (adapting from this issue's original comment):

const format = 'YYYY.MM.MICRO';
const version = '2020.11.3';

// It is november 15th
calver.inc(format, version, 'calendar.micro'); // 2020.11.4 

// It is november 30th
calver.inc(format, version, 'calendar.micro'); // 2020.11.5

// It is december 1st
calver.inc(format, version, 'calendar.micro'); // 2020.12.0

// a later day in december
calver.inc(format, version, 'calendar.micro'); // 2020.12.1
Duncank commented 3 years ago

That's exactly what I found out this week. I've decided to write my own solution that is only suited for the YYYY.MM.MICRO format, maybe it is of use to you:

function getVersionParts(version) {
    const parts = version.split('.');

    return {
        year: +parts[0],
        month: +parts[1],
        minor: +parts[2],
    };
}

function getNewVersion(input) {
    const previousVersion = getVersionParts(input);

    const now = new Date();
    const year = now.getFullYear();
    const month = now.getMonth() + 1;
    let minor = 0;

    if (year === previousVersion.year && month === previousVersion.month) {
        minor = previousVersion.minor + 1;
    }

    return `${year}.${month}.${minor}`;
}

Use it like this:

// in may 2021
getNewVersion('2021.5.3'); // 2021.5.4

// in june 2021
getNewVersion('2021.5.3'); // 2021.6.0
muratgozel commented 3 years ago

Hi @kherock

If you try calver.inc(format, '2021.6.0', 'micro'); instead of calver.inc(format, '2021.6.0', 'calendar.micro'); it will work. Perhaps I should document this better in the readme.

muratgozel commented 3 years ago

Hi @Duncank

Noo I wrote this library to not to write our own functions. Just specify which tag you would like to update in the inc function. Look at the previous message I sent in this issue. Is it still not satisfying you?

kherock commented 3 years ago

@muratgozel My problem isn't that I can't get it to work, this is functionality that used to be possible for incrementing micro versions. I have an automated script that should increment a version number where

You should notice that this is similar to how calendar.<modifier> increments work (though it starts counting from .1 rather than .0). Currently, I'm stuck with writing my own function that looks like this:


function incrementCalendarMicro(format, version) {
  try {
    return calver.inc(format, version, 'calendar');
  } catch (err) {
    if (!err.message.startsWith('There is no change in the version')) throw err;
    return calver.inc(format, version, 'micro');
  }
}
muratgozel commented 3 years ago

A reasonable suggestion for calendar.<semver> tags. Please wait I will implement that shortly.

muratgozel commented 3 years ago

Calendar + semantic tags available in the .inc method as of version 21.1.4

// assuming current date is Jan 2021.
expect(calver.inc('yy.mm.micro.modifier', '20.4.1-dev.3', 'calendar.micro')).toBe('21.1.0')
expect(calver.inc('yy.mm.micro', '21.1.0', 'calendar.micro')).toBe('21.1.1')

Please do npm update.

@kherock @Duncank

daorren commented 3 years ago

I believe there's still a problem

const format = 'YY.MM.MICRO';
console.log(calver.inc(format, '21.1.4', 'micro')) // current package version
// 21.1.5

It's June when you published, why there's an 1 for MM.

muratgozel commented 3 years ago

@daorren The Date.now() function manipulated in test script. It returns a constant date time which is 2021, January.

daorren commented 3 years ago

I believe there's still a problem

const format = 'YY.MM.MICRO';
console.log(calver.inc(format, '21.1.4', 'micro')) // current package version
// 21.1.5

It's June when you published, why there's an 1 for MM.

@muratgozel Now I notice spec/helpers/calver.js, but it should only influence test related things, right? The snippet above is not for test. I mean, if I just require this package and use it, and it should give current month like August, right?

Besides, when I mentiont It's June when you published, why there's an 1 for MM., I was trying to say, when you published this package, you may use dev/commit.js, and it gave you an 1, and that's wrong already if I understood this package correctly

muratgozel commented 3 years ago

@daorren Yes, it is only for tests.

I specified "micro" as a tag therefore it didn't update the calendar portion of the current version. If I were to specify "calendar.micro" then the current version would be 21.8.0.

daorren commented 3 years ago

@daorren Yes, it is only for tests.

I specified "micro" as a tag therefore it didn't update the calendar portion of the current version. If I were to specify "calendar.micro" then the current version would be 21.8.0.

Thank you for your reply, now I understand I must use 'calendar' to update to current date