xyz.rk0cc.willpub.pubspec.parser.PubspecParsePreference Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jpubspec Show documentation
Show all versions of jpubspec Show documentation
Reading and writing pubspec.yaml data and save edited record on Java
package xyz.rk0cc.willpub.pubspec.parser;
import xyz.rk0cc.josev.SemVer;
import xyz.rk0cc.josev.SemVerRangeNode;
import xyz.rk0cc.josev.constraint.pub.PubSemVerConstraint;
import javax.annotation.Nonnull;
import java.util.EnumSet;
import java.util.function.Predicate;
/**
* Preference when exporting {@link xyz.rk0cc.willpub.pubspec.data.Pubspec} to pubspec.yaml
.
*
* All affected preferences must be both {@link #isEnabled(PubspecParsePreference) enabled} and meet required
* {@link xyz.rk0cc.willpub.pubspec.data.PubspecEnvironment#sdk() Dart SDK constraint}. By default, all preferences
* are enabled. Therefore, it provides {@link #enable(PubspecParsePreference)} and
* {@link #disable(PubspecParsePreference)} static method to standardize preferred format of pubspec.yaml
.
*
* @since 1.0.0
*/
public enum PubspecParsePreference {
/**
* Succinct {@link xyz.rk0cc.willpub.pubspec.data.dependencies.type.ThirdPartyHostedReference} data when the package
* name is the same from the hosted repository.
*
* When it enabled, the hosted field will become a URL of repository instead of an object. It's supported since
* Dart 2.15, any older version of Dart SDK in pubspec.yaml
will not apply when export.
*
* Export preview:
* Disabled
*
* foo:
* hosted:
* name: foo
* url: https://example.com
* version: ^1.0.0
*
* Enabled
*
* foo:
* hosted: https://example.com
* version: ^1.0.0
*
*/
SUCCINCT_THIRD_PARTY_HOSTED_FORMAT(sdkc -> {
SemVerRangeNode sdkStart = sdkc.start();
SemVer supportedSince = new SemVer(2, 15);
return sdkStart.orEquals()
? sdkStart.semVer().isGreaterOrEquals(supportedSince)
: sdkStart.semVer().isGreater(supportedSince);
});
/**
* Condition of Dart SDK requirement.
*/
private final Predicate sdkCondition;
/**
* Assign preference with the condition.
*
* @param sdkCondition Giving Dart SDK's {@link PubSemVerConstraint} to check is
* {@link #eligible(PubspecParsePreference, PubSemVerConstraint)} when enabled.
*/
PubspecParsePreference(@Nonnull Predicate sdkCondition) {
this.sdkCondition = sdkCondition;
}
/**
* {@link EnumSet} which contains enabled preferences.
*/
private static final EnumSet enabledPreference
= EnumSet.allOf(PubspecParsePreference.class);
/**
* Enable giving preference and apply it when meeting SDK constraint requirement.
*
* @param preference A preference which want to be activated.
*
* @return true
if modified.
*/
public static boolean enable(@Nonnull PubspecParsePreference preference) {
return enabledPreference.add(preference);
}
/**
* Disable the preference, no matter is met Dart SDK requirement or not.
*
* @param preference A preference which unwanted to apply.
*
* @return true
if modified.
*/
public static boolean disable(@Nonnull PubspecParsePreference preference) {
return enabledPreference.remove(preference);
}
/**
* Providing this preference is enabled currently.
*
* @param preference A preference that want to be checked it's state.
*
* @return true
if enabled already.
*/
public static boolean isEnabled(@Nonnull PubspecParsePreference preference) {
return enabledPreference.contains(preference);
}
/**
* Giving targeted preference with Dart SDK constraint, to determine is eligible to using this preference or not.
*
* @param preference Scoped preference for incoming operation.
* @param sdk Dart SDK constant.
*
* @return true
if enabled and met the constraint requirement.
*/
static boolean eligible(@Nonnull PubspecParsePreference preference, @Nonnull PubSemVerConstraint sdk) {
return isEnabled(preference) && preference.sdkCondition.test(sdk);
}
}