matrei commented on code in PR #14952: URL: https://github.com/apache/grails-core/pull/14952#discussion_r2260379017
########## grails-gradle/common/src/main/groovy/org/apache/grails/gradle/common/PropertyFileUtils.groovy: ########## @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * https://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. + */ +package org.apache.grails.gradle.common + +import groovy.transform.CompileStatic +import org.codehaus.groovy.runtime.IOGroovyMethods + +import java.nio.charset.StandardCharsets +import java.time.LocalDate +import java.time.ZoneOffset +import java.util.regex.Pattern + +@CompileStatic +final class PropertyFileUtils { + private final static Pattern TIME_REGEX = Pattern.compile('^#(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)(?:,|\\s).*$') + + private PropertyFileUtils() { + // prevent instantiation + } + + static ByteArrayInputStream makePropertiesOutputReproducible(ByteArrayOutputStream propertyOutputStream) { + List<String> lines = propertyOutputStream.toString(StandardCharsets.ISO_8859_1.name()).readLines() + + ByteArrayOutputStream toWrite = new ByteArrayOutputStream() + toWrite.withWriter(StandardCharsets.ISO_8859_1.name()) { Writer writer -> + BufferedWriter bufferedWriter = new BufferedWriter(writer) + makePropertyLinesReproducible(lines, bufferedWriter) + bufferedWriter.flush() + } + + new ByteArrayInputStream(toWrite.toByteArray()) + } + + static void makePropertiesFileReproducible(File factoriesFile) { + List<String> lines = factoriesFile.readLines(StandardCharsets.ISO_8859_1.name()) + + factoriesFile.withWriter(StandardCharsets.ISO_8859_1.name()) { BufferedWriter writer -> + makePropertyLinesReproducible(lines, writer) + } + } + + private static void makePropertyLinesReproducible(List<String> lines, BufferedWriter writer) { + String sourceDateEpoch = determineSourceDateEpoch() + + boolean dateReplaced = false + lines.each { String line -> + if (!dateReplaced && TIME_REGEX.matcher(line).matches()) { + dateReplaced = true + IOGroovyMethods.writeLine(writer, "# SOURCE_DATE_EPOCH = ${sourceDateEpoch}" as String) Review Comment: ```suggestion IOGroovyMethods.writeLine(writer, "# SOURCE_DATE_EPOCH = $sourceDateEpoch") ``` ########## grails-forge/buildSrc/src/main/groovy/org/apache/grails/forge/buildlogic/shadowjar/GrailsGroovyExtensionTransformer.groovy: ########## @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * https://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. + */ +package org.apache.grails.forge.buildlogic.shadowjar + +import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer +import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext +import groovy.transform.CompileStatic +import org.apache.grails.gradle.common.PropertyFileUtils +import org.apache.tools.zip.ZipEntry +import org.apache.tools.zip.ZipOutputStream +import org.gradle.api.file.FileTreeElement + +/** + * Based on com.github.jengelman.gradle.plugins.shadow.transformers.GroovyExtensionModuleTransformer but with + * reproducible property file support + */ +@CompileStatic +class GrailsGroovyExtensionTransformer implements Transformer { + + private static final String GROOVY_EXTENSION_MODULE_DESCRIPTOR_PATH = "META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule" Review Comment: ```suggestion private static final String GROOVY_EXTENSION_MODULE_DESCRIPTOR_PATH = 'META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule' ``` ########## grails-gradle/common/build.gradle: ########## @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * https://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. + */ + +plugins { + id 'groovy' + id 'java-library' +} + +version = projectVersion +group = 'org.apache.grails.gradle' + +ext { + pomTitle = 'Grails Gradle Common' + pomDescription = 'Various common utilities that are shared between Grails Gradle code and the Grails Framework' + pomDevelopers = ['jdaugherty': 'James Daugherty'] +} + +dependencies { + compileOnly platform(project(':grails-gradle-bom')) + + // compile with the Groovy version provided by Gradle + // to ensure build compatibility with Gradle, currently Groovy 3.0.x + // see: https://docs.gradle.org/current/userguide/compatibility.html#groovy + compileOnly 'org.codehaus.groovy:groovy' + + // Testing + testImplementation platform(project(':grails-gradle-bom')) + testImplementation 'org.slf4j:slf4j-simple' + testImplementation('org.spockframework:spock-core') { transitive = false } + testImplementation 'org.codehaus.groovy:groovy-test-junit5' + testImplementation 'org.junit.jupiter:junit-jupiter-api' + testImplementation 'org.junit.platform:junit-platform-runner' + + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' + + // for easier setting of environment variables in tests + testImplementation 'uk.org.webcompere:system-stubs-core:2.1.8' Review Comment: Group by configuration scope? ```suggestion // for easier setting of environment variables in tests testImplementation 'uk.org.webcompere:system-stubs-core:2.1.8' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' ``` ########## grails-gradle/common/src/main/groovy/org/apache/grails/gradle/common/PropertyFileUtils.groovy: ########## @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * https://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. + */ +package org.apache.grails.gradle.common + +import groovy.transform.CompileStatic +import org.codehaus.groovy.runtime.IOGroovyMethods + +import java.nio.charset.StandardCharsets +import java.time.LocalDate +import java.time.ZoneOffset +import java.util.regex.Pattern + +@CompileStatic +final class PropertyFileUtils { + private final static Pattern TIME_REGEX = Pattern.compile('^#(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)(?:,|\\s).*$') + + private PropertyFileUtils() { + // prevent instantiation + } + + static ByteArrayInputStream makePropertiesOutputReproducible(ByteArrayOutputStream propertyOutputStream) { + List<String> lines = propertyOutputStream.toString(StandardCharsets.ISO_8859_1.name()).readLines() + + ByteArrayOutputStream toWrite = new ByteArrayOutputStream() + toWrite.withWriter(StandardCharsets.ISO_8859_1.name()) { Writer writer -> + BufferedWriter bufferedWriter = new BufferedWriter(writer) + makePropertyLinesReproducible(lines, bufferedWriter) + bufferedWriter.flush() + } + + new ByteArrayInputStream(toWrite.toByteArray()) + } + + static void makePropertiesFileReproducible(File factoriesFile) { + List<String> lines = factoriesFile.readLines(StandardCharsets.ISO_8859_1.name()) + + factoriesFile.withWriter(StandardCharsets.ISO_8859_1.name()) { BufferedWriter writer -> + makePropertyLinesReproducible(lines, writer) + } + } + + private static void makePropertyLinesReproducible(List<String> lines, BufferedWriter writer) { + String sourceDateEpoch = determineSourceDateEpoch() + + boolean dateReplaced = false + lines.each { String line -> + if (!dateReplaced && TIME_REGEX.matcher(line).matches()) { + dateReplaced = true + IOGroovyMethods.writeLine(writer, "# SOURCE_DATE_EPOCH = ${sourceDateEpoch}" as String) + return + } + + writer.writeLine(line) + } + } + + private static String determineSourceDateEpoch() { + String sourceDateEpoch = System.getenv('SOURCE_DATE_EPOCH') + if (!sourceDateEpoch) { + sourceDateEpoch = LocalDate.now(ZoneOffset.UTC) + .atStartOfDay(ZoneOffset.UTC) + .toEpochSecond().toString() + } + sourceDateEpoch Review Comment: ```suggestion System.getenv('SOURCE_DATE_EPOCH') ?: LocalDate.now(ZoneOffset.UTC) .atStartOfDay(ZoneOffset.UTC) .toEpochSecond().toString() ``` ########## grails-gradle/common/build.gradle: ########## @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * https://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. + */ + +plugins { + id 'groovy' + id 'java-library' +} + +version = projectVersion +group = 'org.apache.grails.gradle' + +ext { + pomTitle = 'Grails Gradle Common' + pomDescription = 'Various common utilities that are shared between Grails Gradle code and the Grails Framework' + pomDevelopers = ['jdaugherty': 'James Daugherty'] +} + +dependencies { + compileOnly platform(project(':grails-gradle-bom')) + + // compile with the Groovy version provided by Gradle + // to ensure build compatibility with Gradle, currently Groovy 3.0.x + // see: https://docs.gradle.org/current/userguide/compatibility.html#groovy + compileOnly 'org.codehaus.groovy:groovy' + + // Testing + testImplementation platform(project(':grails-gradle-bom')) + testImplementation 'org.slf4j:slf4j-simple' + testImplementation('org.spockframework:spock-core') { transitive = false } + testImplementation 'org.codehaus.groovy:groovy-test-junit5' + testImplementation 'org.junit.jupiter:junit-jupiter-api' + testImplementation 'org.junit.platform:junit-platform-runner' Review Comment: Do we need these? ```suggestion ``` ########## grails-gradle/common/src/main/groovy/org/apache/grails/gradle/common/PropertyFileUtils.groovy: ########## @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * https://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. + */ +package org.apache.grails.gradle.common + +import groovy.transform.CompileStatic +import org.codehaus.groovy.runtime.IOGroovyMethods + +import java.nio.charset.StandardCharsets +import java.time.LocalDate +import java.time.ZoneOffset +import java.util.regex.Pattern + +@CompileStatic +final class PropertyFileUtils { + private final static Pattern TIME_REGEX = Pattern.compile('^#(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)(?:,|\\s).*$') Review Comment: Use Groovy pattern syntax? ```suggestion private final static Pattern TIME_REGEX = ~'^#(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)(?:,|\\s).*$' ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
