Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move PhotonVersion to C++ file #949

Merged
merged 4 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,4 @@ photonlib-cpp-examples/*/vendordeps/*
*/networktables.json
*.sqlite
photon-server/src/main/resources/web/index.html
photon-lib/src/generate/native/cpp/PhotonVersion.cpp
5 changes: 3 additions & 2 deletions photon-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ model {
cpp {
source {
srcDirs "src/main/native/cpp"
srcDirs "src/generate/native/cpp"
include "**/*.cpp"
}
exportedHeaders {
Expand Down Expand Up @@ -140,8 +141,8 @@ task writeCurrentVersion {
def versionFileIn = file("${rootDir}/shared/PhotonVersion.java.in")
writePhotonVersionFile(versionFileIn, Path.of("$projectDir", "src", "main", "java", "org", "photonvision", "PhotonVersion.java"),
versionString)
versionFileIn = file("${rootDir}/shared/PhotonVersion.h.in")
writePhotonVersionFile(versionFileIn, Path.of("$projectDir", "src", "generate", "native", "include", "PhotonVersion.h"),
versionFileIn = file("${rootDir}/shared/PhotonVersion.cpp.in")
writePhotonVersionFile(versionFileIn, Path.of("$projectDir", "src", "generate", "native", "cpp", "PhotonVersion.cpp"),
versionString)
}

Expand Down
7 changes: 2 additions & 5 deletions photon-lib/publish.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
apply plugin: 'maven-publish'

ext.licenseFile = files("$rootDir/LICENSE")
ext.photonVersionFile = files("$projectDir/src/generate/native/include")

def outputsFolder = file("$buildDir/outputs")
def allOutputsFolder = file("$buildDir/allOutputs")
Expand Down Expand Up @@ -51,11 +50,9 @@ task cppHeadersZip(type: Zip) {
archiveBaseName = zipBaseName
classifier = "headers"

from(licenseFile) {
into '/'
}
duplicatesStrategy = "warn"

from(photonVersionFile) {
from(licenseFile) {
into '/'
}

Expand Down
16 changes: 16 additions & 0 deletions photon-lib/src/main/native/cpp/photonlib/PhotonCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ std::optional<cv::Mat> PhotonCamera::GetDistCoeffs() {
return std::nullopt;
}

static bool VersionMatches(std::string them_str) {
std::smatch match;
std::regex versionPattern{"v[0-9]+.[0-9]+.[0-9]+"};

std::string us_str = PhotonVersion::versionString;

// Check that both versions are in the right format
if (std::regex_search(us_str, match, versionPattern) &&
std::regex_search(them_str, match, versionPattern)) {
// If they are, check string equality
return (us_str == them_str);
} else {
return false;
}
}

void PhotonCamera::VerifyVersion() {
if (!PhotonCamera::VERSION_CHECK_ENABLED) return;

Expand Down
36 changes: 36 additions & 0 deletions photon-lib/src/main/native/include/PhotonVersion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* MIT License
*
* Copyright (c) PhotonVision
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#pragma once

#include <regex>
#include <string>

namespace photonlib {
namespace PhotonVersion {
extern const char* versionString;
extern const char* buildDate;
extern const bool isRelease;
} // namespace PhotonVersion
} // namespace photonlib
32 changes: 32 additions & 0 deletions photon-lib/src/test/native/cpp/VersionTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* MIT License
*
* Copyright (c) PhotonVision
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <iostream>

#include "PhotonVersion.h"
#include "gtest/gtest.h"

TEST(VersionTest, PrintVersion) {
std::cout << photonlib::PhotonVersion::versionString << std::endl;
}
21 changes: 5 additions & 16 deletions shared/PhotonVersion.h.in → shared/PhotonVersion.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,12 @@
* regenerated any time the publish task is run, or when this file is deleted.
*/

static const char* dev_ = "dev";

namespace photonlib {
namespace PhotonVersion {
const std::string versionString = "${version}";
const std::string buildDate = "${date}";
const bool isRelease = !(versionString.rfind("dev", 0) == 0);
}

bool VersionMatches(const std::string& other) {
std::smatch match;
std::regex versionPattern{"v[0-9]+.[0-9]+.[0-9]+"};
// Check that both versions are in the right format
if (std::regex_search(PhotonVersion::versionString, match, versionPattern) &&
std::regex_search(other, match, versionPattern)) {
// If they are, check string equality
return (PhotonVersion::versionString == other);
} else {
return false;
}
const char* versionString = "${version}";
const char* buildDate = "${date}";
const bool isRelease = strncmp(dev_, versionString, strlen(dev_)) != 0;
}
}