Skip to content

Commit c493c0e

Browse files
committed
Add AppInfo class
1 parent 59921e7 commit c493c0e

File tree

8 files changed

+133
-0
lines changed

8 files changed

+133
-0
lines changed

src/global/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@ set(MODULE_URI Global)
33
set(MODULE_SRC
44
globalmodule.cpp
55
globalmodule.h
6+
iappinfo.h
67
modularity/ioc.h
78
modularity/modulesioc.h
89
modularity/imoduleexportinterface.h
910
modularity/imodulesetup.h
11+
internal/appinfo.cpp
12+
internal/appinfo.h
1013
)
1114

1215
include(${PROJECT_SOURCE_DIR}/build/module.cmake)
1316

17+
include(FetchContent)
18+
FetchContent_Declare(cmake_git_version_tracking
19+
GIT_REPOSITORY https://github.com/andrew-hardin/cmake-git-version-tracking.git
20+
GIT_TAG 904dbda1336ba4b9a1415a68d5f203f576b696bb
21+
)
22+
FetchContent_MakeAvailable(cmake_git_version_tracking)
23+
24+
target_link_libraries(${MODULE} PRIVATE cmake_git_version_tracking)
25+
string(TIMESTAMP BUILD_YEAR "%Y")
26+
target_compile_definitions(${MODULE} PRIVATE BUILD_YEAR=${BUILD_YEAR})
27+
1428
add_subdirectory(test)

src/global/globalmodule.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
// SPDX-License-Identifier: GPL-3.0-or-later
22

3+
#include <QQmlEngine>
4+
35
#include "globalmodule.h"
6+
#include "internal/appinfo.h"
47

58
using namespace scratchcpp;
69

710
std::string GlobalModule::moduleName() const
811
{
912
return "global";
1013
}
14+
15+
void GlobalModule::registerExports()
16+
{
17+
m_appInfo = std::make_shared<AppInfo>();
18+
19+
QQmlEngine::setObjectOwnership(m_appInfo.get(), QQmlEngine::CppOwnership);
20+
qmlRegisterSingletonInstance<AppInfo>("ScratchCPP.Ui", 1, 0, "AppInfo", m_appInfo.get());
21+
modularity::ioc()->registerExport<IAppInfo>(m_appInfo);
22+
}

src/global/globalmodule.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@
22

33
#pragma once
44

5+
#include <memory>
6+
57
#include "modularity/imodulesetup.h"
68

79
namespace scratchcpp
810
{
911

12+
class AppInfo;
13+
1014
class GlobalModule : public modularity::IModuleSetup
1115
{
1216
public:
1317
std::string moduleName() const override;
18+
19+
void registerExports() override;
20+
21+
private:
22+
std::shared_ptr<AppInfo> m_appInfo;
1423
};
1524

1625
} // namespace scratchcpp

src/global/iappinfo.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include "modularity/ioc.h"
6+
7+
class QString;
8+
9+
namespace scratchcpp
10+
{
11+
12+
class IAppInfo : MODULE_EXPORT_INTERFACE
13+
{
14+
public:
15+
virtual ~IAppInfo() { }
16+
17+
virtual QString revision() const = 0;
18+
virtual int buildYear() const = 0;
19+
};
20+
21+
} // namespace scratchcpp

src/global/internal/appinfo.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
#include <git.h>
4+
5+
#include "appinfo.h"
6+
7+
using namespace scratchcpp;
8+
9+
AppInfo::AppInfo(QObject *parent) :
10+
QObject(parent)
11+
{
12+
}
13+
14+
QString scratchcpp::AppInfo::revision() const
15+
{
16+
return git_CommitSHA1();
17+
}
18+
19+
int scratchcpp::AppInfo::buildYear() const
20+
{
21+
return BUILD_YEAR;
22+
}

src/global/internal/appinfo.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include <QObject>
6+
7+
#include "iappinfo.h"
8+
9+
namespace scratchcpp
10+
{
11+
12+
class AppInfo
13+
: public QObject
14+
, public IAppInfo
15+
{
16+
Q_OBJECT
17+
public:
18+
explicit AppInfo(QObject *parent = nullptr);
19+
20+
Q_INVOKABLE QString revision() const override;
21+
Q_INVOKABLE int buildYear() const override;
22+
};
23+
24+
} // namespace scratchcpp

src/global/test/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
set(MODULE_TEST_SRC
22
modularity.cpp
33
setup.cpp
4+
appinfo.cpp
45
fakeexport.h
56
fakedependency.h
67
mocks/moduleexportinterfacemock.h
78
mocks/modulesetupmock.h
89
)
910

1011
include(${PROJECT_SOURCE_DIR}/build/module_test.cmake)
12+
13+
set(TARGET ${MODULE}_test)
14+
target_link_libraries(${TARGET} cmake_git_version_tracking)
15+
string(TIMESTAMP BUILD_YEAR "%Y")
16+
target_compile_definitions(${TARGET} PRIVATE BUILD_YEAR=${BUILD_YEAR})

src/global/test/appinfo.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <gtest/gtest.h>
2+
#include <QDebug>
3+
4+
#include "internal/appinfo.h"
5+
6+
// Workaround for multiple definition error
7+
namespace scratchcpp::test
8+
{
9+
#include <git.h>
10+
}
11+
12+
using namespace scratchcpp;
13+
using namespace scratchcpp::test;
14+
15+
TEST(AppInfoTest, Revision)
16+
{
17+
AppInfo info;
18+
ASSERT_EQ(info.revision(), git_CommitSHA1());
19+
}
20+
21+
TEST(AppInfoTest, BuildYear)
22+
{
23+
AppInfo info;
24+
ASSERT_EQ(info.buildYear(), BUILD_YEAR);
25+
}

0 commit comments

Comments
 (0)