|
24 | 24 | import java.io.InputStream; |
25 | 25 | import java.io.UncheckedIOException; |
26 | 26 | import java.net.URL; |
| 27 | +import java.nio.charset.StandardCharsets; |
27 | 28 | import java.nio.file.Files; |
28 | 29 | import java.nio.file.LinkOption; |
29 | 30 | import java.nio.file.Path; |
|
44 | 45 | import org.slf4j.Logger; |
45 | 46 | import org.slf4j.LoggerFactory; |
46 | 47 | import tools.jackson.databind.JsonNode; |
| 48 | +import tools.jackson.databind.node.ObjectNode; |
47 | 49 |
|
48 | 50 | import com.vaadin.experimental.CoreFeatureFlagProvider; |
49 | 51 | import com.vaadin.flow.di.Lookup; |
50 | 52 | import com.vaadin.flow.di.ResourceProvider; |
51 | 53 | import com.vaadin.flow.function.DeploymentConfiguration; |
52 | 54 | import com.vaadin.flow.internal.DevModeHandler; |
53 | 55 | import com.vaadin.flow.internal.DevModeHandlerManager; |
| 56 | +import com.vaadin.flow.internal.JacksonUtils; |
54 | 57 | import com.vaadin.flow.internal.Pair; |
55 | 58 | import com.vaadin.flow.internal.StringUtil; |
56 | 59 | import com.vaadin.flow.internal.hilla.EndpointRequestUtil; |
57 | 60 | import com.vaadin.flow.internal.menu.MenuRegistry; |
58 | 61 | import com.vaadin.flow.server.AbstractConfiguration; |
59 | 62 | import com.vaadin.flow.server.Constants; |
| 63 | +import com.vaadin.flow.server.Platform; |
60 | 64 | import com.vaadin.flow.server.VaadinService; |
61 | 65 | import com.vaadin.flow.server.VaadinServlet; |
62 | 66 | import com.vaadin.flow.server.frontend.scanner.ClassFinder; |
@@ -1560,4 +1564,137 @@ public static boolean isTailwindCssEnabled(Options options) { |
1560 | 1564 | .isEnabled(CoreFeatureFlagProvider.TAILWIND_CSS); |
1561 | 1565 | } |
1562 | 1566 |
|
| 1567 | + /** |
| 1568 | + * Compares current platform version with the one last recorded as installed |
| 1569 | + * in node_modules/.vaadin/vaadin_version. In case there was no existing |
| 1570 | + * platform version recorder and node_modules exists, then platform is |
| 1571 | + * considered as staying on the same version. |
| 1572 | + * |
| 1573 | + * @param finder |
| 1574 | + * project execution class finder |
| 1575 | + * @param npmFolder |
| 1576 | + * npm root folder |
| 1577 | + * @param nodeModules |
| 1578 | + * node_modules folder |
| 1579 | + * @param buildDirectory |
| 1580 | + * project build directory, to find dev-bundle folder |
| 1581 | + * @return {@code true} if the version has changed, {@code false} if not |
| 1582 | + * @throws IOException |
| 1583 | + * when file reading fails |
| 1584 | + */ |
| 1585 | + protected static boolean isPlatformMajorVersionUpdated(ClassFinder finder, |
| 1586 | + File npmFolder, File nodeModules, File buildDirectory) |
| 1587 | + throws IOException { |
| 1588 | + // if no record of current version is present, version is not |
| 1589 | + // considered updated |
| 1590 | + Optional<String> platformVersion = getVaadinVersion(finder); |
| 1591 | + if (platformVersion.isPresent()) { |
| 1592 | + JsonNode vaadinJsonContents = getBundleVaadinContent( |
| 1593 | + buildDirectory); |
| 1594 | + if (!vaadinJsonContents.has(NodeUpdater.VAADIN_VERSION) |
| 1595 | + && nodeModules.exists()) { |
| 1596 | + // Check for vaadin version from installed node_modules |
| 1597 | + vaadinJsonContents = getVaadinJsonContents(npmFolder); |
| 1598 | + } |
| 1599 | + // If no record of previous version, version is considered same |
| 1600 | + if (!vaadinJsonContents.has(NodeUpdater.VAADIN_VERSION)) { |
| 1601 | + return false; |
| 1602 | + } |
| 1603 | + FrontendVersion jsonVersion = new FrontendVersion(vaadinJsonContents |
| 1604 | + .get(NodeUpdater.VAADIN_VERSION).asString()); |
| 1605 | + FrontendVersion platformsVersion = new FrontendVersion( |
| 1606 | + platformVersion.get()); |
| 1607 | + return jsonVersion.getMajorVersion() != platformsVersion |
| 1608 | + .getMajorVersion(); |
| 1609 | + } |
| 1610 | + return false; |
| 1611 | + } |
| 1612 | + |
| 1613 | + private static JsonNode getBundleVaadinContent(File buildDirectory) |
| 1614 | + throws IOException { |
| 1615 | + JsonNode vaadinJsonContents; |
| 1616 | + File vaadinJsonFile = new File( |
| 1617 | + new File(buildDirectory, Constants.DEV_BUNDLE_LOCATION), |
| 1618 | + TaskRunDevBundleBuild.VAADIN_JSON); |
| 1619 | + if (!vaadinJsonFile.exists()) { |
| 1620 | + return JacksonUtils.createObjectNode(); |
| 1621 | + } |
| 1622 | + String fileContent = Files.readString(vaadinJsonFile.toPath(), |
| 1623 | + StandardCharsets.UTF_8); |
| 1624 | + vaadinJsonContents = JacksonUtils.readTree(fileContent); |
| 1625 | + return vaadinJsonContents; |
| 1626 | + } |
| 1627 | + |
| 1628 | + /** |
| 1629 | + * Compares current platform version with the one last recorded as installed |
| 1630 | + * in node_modules/.vaadin/vaadin_version. In case there was no existing |
| 1631 | + * platform version recorder and node_modules exists, then platform is |
| 1632 | + * considered updated. |
| 1633 | + * |
| 1634 | + * @param finder |
| 1635 | + * project execution class finder |
| 1636 | + * @param npmFolder |
| 1637 | + * npm root folder |
| 1638 | + * @param nodeModules |
| 1639 | + * node_modules folder |
| 1640 | + * @return {@code true} if the version has changed, {@code false} if not |
| 1641 | + * @throws IOException |
| 1642 | + * when file reading fails |
| 1643 | + */ |
| 1644 | + protected static boolean isPlatformVersionUpdated(ClassFinder finder, |
| 1645 | + File npmFolder, File nodeModules) throws IOException { |
| 1646 | + // if no record of current version is present, version is not |
| 1647 | + // considered updated |
| 1648 | + Optional<String> platformVersion = getVaadinVersion(finder); |
| 1649 | + if (platformVersion.isPresent() && nodeModules.exists()) { |
| 1650 | + JsonNode vaadinJsonContents = getVaadinJsonContents(npmFolder); |
| 1651 | + // If no record of previous version, version is considered updated |
| 1652 | + if (!vaadinJsonContents.has(NodeUpdater.VAADIN_VERSION)) { |
| 1653 | + return true; |
| 1654 | + } |
| 1655 | + return !Objects.equals(vaadinJsonContents |
| 1656 | + .get(NodeUpdater.VAADIN_VERSION).asString(), |
| 1657 | + platformVersion.get()); |
| 1658 | + } |
| 1659 | + return false; |
| 1660 | + } |
| 1661 | + |
| 1662 | + protected static Optional<String> getVaadinVersion(ClassFinder finder) { |
| 1663 | + URL coreVersionsResource = finder |
| 1664 | + .getResource(Constants.VAADIN_CORE_VERSIONS_JSON); |
| 1665 | + |
| 1666 | + if (coreVersionsResource == null) { |
| 1667 | + return Optional.empty(); |
| 1668 | + } |
| 1669 | + try (InputStream vaadinVersionsStream = coreVersionsResource |
| 1670 | + .openStream()) { |
| 1671 | + final JsonNode versionsJson = JacksonUtils |
| 1672 | + .readTree(StringUtil.toUTF8String(vaadinVersionsStream)); |
| 1673 | + if (versionsJson.has("platform")) { |
| 1674 | + return Optional.of(versionsJson.get("platform").asString()); |
| 1675 | + } |
| 1676 | + } catch (Exception e) { |
| 1677 | + LoggerFactory.getLogger(Platform.class) |
| 1678 | + .error("Unable to determine version information", e); |
| 1679 | + } |
| 1680 | + |
| 1681 | + return Optional.empty(); |
| 1682 | + } |
| 1683 | + |
| 1684 | + static File getVaadinJsonFile(File npmFolder) { |
| 1685 | + return new File(new File(npmFolder, NODE_MODULES), |
| 1686 | + NodeUpdater.VAADIN_JSON); |
| 1687 | + } |
| 1688 | + |
| 1689 | + static ObjectNode getVaadinJsonContents(File npmFolder) throws IOException { |
| 1690 | + File vaadinJsonFile = getVaadinJsonFile(npmFolder); |
| 1691 | + if (vaadinJsonFile.exists()) { |
| 1692 | + String fileContent = Files.readString(vaadinJsonFile.toPath(), |
| 1693 | + StandardCharsets.UTF_8); |
| 1694 | + return JacksonUtils.readTree(fileContent); |
| 1695 | + } else { |
| 1696 | + return JacksonUtils.createObjectNode(); |
| 1697 | + } |
| 1698 | + } |
| 1699 | + |
1563 | 1700 | } |
0 commit comments